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

import cn.com.victorysoft.business.condition.dto.AdjustPlanSummary;
import cn.com.victorysoft.business.condition.dto.AdjustPlanSummaryType;
import cn.com.victorysoft.business.condition.entity.AdjustPlan;
import cn.com.victorysoft.business.condition.entity.Gzlx;
import cn.com.victorysoft.business.condition.param.AdjustPlanExportParam;
import cn.com.victorysoft.business.condition.param.AdjustPlanParam;
import cn.com.victorysoft.business.condition.param.GzlxParam;
import cn.com.victorysoft.business.condition.service.AdjustPlanService;
import cn.com.victorysoft.business.sys.entity.ExportResult;
import cn.com.victorysoft.business.sys.param.*;
import cn.com.victorysoft.business.util.DateUtils;
import cn.com.victorysoft.business.util.ExcelUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Api(tags = "分注井测调计划安排")
@RestController
@RequestMapping("condition/adjustPlan")
public class AdjustPlanController {
    @Resource
    private AdjustPlanService adjustPlanService;

    @GetMapping("/page")
    @ApiOperation("查询分页数据")
    public PageInfo<AdjustPlan> Page(AdjustPlanParam param) {
        return adjustPlanService.selectPage(param);
    }

    @GetMapping("/gzlx")
    @ApiOperation("查询管柱类型")
    public List<Gzlx> selectGzlx(GzlxParam param) {
        return adjustPlanService.selectGzlx(param);
    }

    @SneakyThrows
    @PostMapping("/export")
    @ApiOperation("导出数据")
    public ExportResult selectListExport(@RequestBody AdjustPlanExportParam param) {
        List<AdjustPlan> list = adjustPlanService.selectList(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @PostMapping("/insert")
    @ApiOperation("插入")
    public int insert(@RequestBody AdjustPlan param) {
        AdjustPlan record = adjustPlanService.selectByPrimaryKey(param);
        if (record != null) {
            return -1;
        } else {
            return adjustPlanService.insert(param);
        }

    }

    @PutMapping("/update")
    @ApiOperation("更新")
    public int update(@RequestBody AdjustPlan param) {
        return adjustPlanService.update(param);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public int delete(AdjustPlan param) {
        return adjustPlanService.delete(param);
    }

    @GetMapping("/generate")
    @ApiOperation("根据井号生成数据")
    public AdjustPlan generate(String wellname, String month) {
        return adjustPlanService.generateByWellname(wellname, month);
    }

    @GetMapping("/summary")
    @ApiOperation("测调完成情况")
    public List<AdjustPlanSummary> summary(UnitMonthParam param) {
        return adjustPlanService.summary(param);
    }

    @SneakyThrows
    @GetMapping("/summaryType")
    @ApiOperation("上月测调完成情况分类")
    public List<AdjustPlanSummaryType> summaryType(UnitMonthParam param) {
        // 取上月
        Date firstDate = DateUtils.parseDate(param.getMonth() + "01", "yyyyMMdd");
        // 上月最后一天
        Date endDate = DateUtils.addDays(firstDate, -1);
        param.setMonth(DateUtils.format(endDate, "yyyyMM"));
        return adjustPlanService.summaryType(param);
    }

    @PostMapping("/summaryTypeExport")
    @ApiOperation("测调完成情况分类导出")
    public ExportResult summaryTypeExport(@RequestBody UnitMonthExportParam param) {
        List<AdjustPlanSummaryType> list = this.summaryType(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

}
