package cn.com.victorysoft.business.indicator.controller;

import cn.com.victorysoft.business.indicator.entity.ObjectTypes;
import cn.com.victorysoft.business.indicator.entity.QualityCheck;
import cn.com.victorysoft.business.indicator.entity.WaterStation;
import cn.com.victorysoft.business.indicator.param.*;
import cn.com.victorysoft.business.indicator.service.QualityCheckService;
import cn.com.victorysoft.business.sys.dto.Result;
import cn.com.victorysoft.business.sys.entity.ExportResult;
import cn.com.victorysoft.business.sys.entity.WellBasic;
import cn.com.victorysoft.business.sys.param.UnitFieldBlockWellParam;
import cn.com.victorysoft.business.util.ExcelUtils;
import cn.com.victorysoft.business.util.PathUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Api(tags = "水质管理")
@RestController
@RequestMapping("indicator/qualityCheck")
public class QualityCheckController {
    @Resource
    private QualityCheckService qualityCheckService;

    @GetMapping("/page")
    @ApiOperation("查询水质检测维护分页数据")
    public PageInfo<QualityCheck> Page(QualityCheckParam param) {
        return qualityCheckService.selectPage(param);
    }

    @GetMapping("/list")
    @ApiOperation("查询阶段水质检测查询分页数据")
    public PageInfo<QualityCheck> searchList(QualityCheckSearchParam param) {

        return qualityCheckService.selectSearchPage(param);
    }

    @PostMapping("/export")
    @ApiOperation("导出数据")
    public ExportResult selectListExport(@RequestBody QualityCheckExportParam param) {
        List<QualityCheck> list = qualityCheckService.selectSearchList(param);
        ExportResult exportResult = ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
        String url = exportResult.getUrl();
        String filePath = PathUtils.getExportPath() + "/" + url;
        File file = new File(filePath);
        try {
            FileInputStream inputStream = new FileInputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            for (int i = 2; i < sheet.getLastRowNum() + 1; i++) {
                XSSFRow row = sheet.getRow(i);
                String stringCellValue = row.getCell(0).getStringCellValue();
                if ("标准值".equals(stringCellValue) || "平均值".equals(stringCellValue) || "达标率%".equals(stringCellValue) || "不达标数平均值".equals(stringCellValue)) {
                    CellRangeAddress region1 = new CellRangeAddress(i, i, 4, 5);
                    CellRangeAddress region2 = new CellRangeAddress(i, i, 6, 7);
                    CellRangeAddress region3 = new CellRangeAddress(i, i, 8, 9);
                    CellRangeAddress region4 = new CellRangeAddress(i, i, 10, 11);
                    CellRangeAddress region5 = new CellRangeAddress(i, i, 12, 13);
                    CellRangeAddress region6 = new CellRangeAddress(i, i, 14, 15);
                    sheet.addMergedRegion(region1);
                    sheet.addMergedRegion(region2);
                    sheet.addMergedRegion(region3);
                    sheet.addMergedRegion(region4);
                    sheet.addMergedRegion(region5);
                    sheet.addMergedRegion(region6);
                }
            }
            inputStream.close();
            FileOutputStream fout = new FileOutputStream(file);
            workbook.write(fout);
            fout.flush();
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return exportResult;
    }

    @GetMapping("/typeList")
    @ApiOperation("查询对象属性")
    public List<ObjectTypes> selectTypes() {
        return qualityCheckService.selectTypes();
    }

    @GetMapping("/waterStationList")
    @ApiOperation("查询联合站/污水站/注水站/配水间")
    public List<WaterStation> stationList(WaterStationParam param) {
        return qualityCheckService.selectWaterStation(param);
    }

    @GetMapping("/waterWellList")
    @ApiOperation("查询单位/油田/区块的水井井号")
    public List<WellBasic> waterWellList(UnitFieldBlockWellParam param) {
        return qualityCheckService.selectWaterWellTop20(param);
    }

    @PostMapping("/save")
    @ApiOperation("水质检测增加")
    public int save(@RequestBody QualityCheck param) {
        int flage = 9;
        QualityCheck qualityCheck= qualityCheckService.selectByPrimaryKey(param);
        if (null != qualityCheck) {
            flage = -1;
        } else {
            flage = qualityCheckService.insert(param);
        }
        return flage;
    }

    @PutMapping("/update")
    @ApiOperation("水质检测修改")
    public int update(@RequestBody QualityCheck param) {
        return qualityCheckService.updateData(param);
    }

    @DeleteMapping("/delete")
    @ApiOperation("水质检测删除")
    public int delete(QualityCheckDelete param) {
        return qualityCheckService.deleteByDate(param);
    }

    @GetMapping("/template")
    @ApiOperation("模板下载")
    public void template(HttpServletResponse response) throws IOException {
        qualityCheckService.downloadTemplate("水质检测导入模板.xlsx", response);
    }

    @PostMapping("/upload")
    @ApiOperation("导入")
    public Result upload(MultipartFile file) {
        return qualityCheckService.upload(file);
    }
}
