package com.nantian.cloud.dbcapp.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.entity.ConfigureDataCollect;
import com.nantian.cloud.dbcapp.entity.ConfigureDataCollectEnable;
import com.nantian.cloud.dbcapp.entity.ConfigureDataCollectEnableApply;
import com.nantian.cloud.dbcapp.service.ConfigureDataCollectEnableApplyService;
import com.nantian.cloud.dbcapp.service.ConfigureDataCollectEnableService;
import com.nantian.cloud.dbcapp.service.ConfigureDataCollectService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/configureDataCollectEnable")
public class ConfigureDataCollectEnableController {
    @Autowired
    private ConfigureDataCollectEnableService configureDataCollectEnableService;
    @Autowired
    private ConfigureDataCollectEnableApplyService enableApplyService;
    @Autowired
    private ConfigureDataCollectService configureDataCollectService;

    @PostMapping("/renewApply")
    @SysLog("更新采集配置项停用申请")
    @ApiOperation(value = "更新采集配置项停用申请", notes = "更新采集配置项停用申请")
    public R addApply(@RequestBody ConfigureDataCollectEnableApply enableApply) {
        ConfigureDataCollectEnableApply old = enableApplyService.getOldByApply(enableApply);
        if (old != null) {
            if ("0".equals(old.getAuditType())) {
                //停用记录已审核通过
                return R.failed(old.getCollectItemName() + "停用开始时间为 " + old.getStartTime() + " 的停用申请记录已审核通过，请勿重复提交！");
            } else {
                //同一采集点，同一开始停用时间的停用记录，为未审核或审核退回，进行停用记录覆盖
                enableApply.setId(old.getId());
            }
        }
        ScUser user = SecurityUtils.getUser();
        if (enableApply.getId() == null) {
            enableApply.setCreatTime(new Date());
            enableApply.setCreatUserId(user.getId());
            enableApply.setDelFlag("1");
        } else {
            enableApply.setUpdateTime(new Date());
            enableApply.setUpdateUserId(user.getId());
        }
        boolean b = enableApplyService.saveOrUpdate(enableApply);
        return b ? R.ok("更新采集配置项停用申请成功！") : R.failed("更新采集配置项停用申请失败！");
    }

    @GetMapping("/applyPage")
    @SysLog("查询采集配置项停用申请")
    @ApiOperation(value = "查询采集配置项停用申请", notes = "查询采集配置项停用申请")
    public R applyList(Page page, ConfigureDataCollectEnableApply enableApply) {
        return R.ok(enableApplyService.getApplyPage(page, enableApply));
    }

    @GetMapping("/delApply")
    @SysLog("删除采集配置项停用申请")
    @ApiOperation(value = "删除采集配置项停用申请", notes = "删除采集配置项停用申请")
    public R delApply(Integer id) {
        boolean b = enableApplyService.delApply(id);
        return b ? R.ok("删除采集配置项停用申请成功！") : R.failed("删除采集配置项停用申请失败！");
    }

    @PostMapping("/auditApply")
    @SysLog("政府审批采集配置项停用申请")
    @ApiOperation(value = "政府审批采集配置项停用申请", notes = "政府审批采集配置项停用申请")
    public R auditApply(@RequestBody List<ConfigureDataCollectEnableApply> applyList) {
        List<ConfigureDataCollectEnableApply> tmpList = new ArrayList<>();
        ScUser user = SecurityUtils.getUser();
        for (ConfigureDataCollectEnableApply enableApply : applyList) {
            enableApply.setAuditTime(new Date());
            enableApply.setAuditUser(user.getUsername());
            if ("0".equals(enableApply.getAuditType())) { //政府审核通过，需要更新采集配置项停用记录
                tmpList.add(enableApply);
            }
        }
        boolean b1 = enableApplyService.updateBatchById(applyList);
        boolean b2 = enableApplyService.addEnableByApply(tmpList);
        //确认停用开始日期，删除停用开始日期之后的异常数据，将异常数据做标记，备注为“异常数据，因采集配置项停用被删除”
        for (ConfigureDataCollectEnableApply enableApply : tmpList) {
            boolean b3 = enableApplyService.updateEnergyExceptionDataInfoByApplyStartTime(
                    enableApply.getEnterpriseCode(),
                    enableApply.getDataCode(),
                    enableApply.getStartTime());

        }

        return b1 & b2 ? R.ok("审批采集配置项停用申请成功！") : R.failed("审批采集配置项停用申请失败！");
    }

    @GetMapping("/getEnableList")
    @SysLog("获取企业采集配置项")
    @ApiOperation(value = "获取企业采集配置项", notes = "获取企业采集配置项")
    @ApiImplicitParam(name = "enterpriseCode", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getEnableList(String enterpriseCode, String dataCode, long current, long size) {
        Page<Object> page = new Page<>(current, size);
        if (StringUtils.isEmpty(enterpriseCode)) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        return R.ok(configureDataCollectEnableService.getEnableList(page, enterpriseCode, dataCode));
    }

    @PostMapping("/addEnable")
    @SysLog("企业采集配置项停用设置")
    @ApiOperation(value = "企业采集配置项修改记录", notes = "企业采集配置项修改记录")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R addEnable(@RequestBody ConfigureDataCollectEnable configureDataCollectEnable) {
        //是否停用：0-未停用，1-停用
        configureDataCollectEnable.setIsEnable("1");
        configureDataCollectEnable.setCreatTime(new Date());
        ScUser user = SecurityUtils.getUser();
        configureDataCollectEnable.setCreatUserId(user.getId());
        if (configureDataCollectEnableService.saveOrUpdate(configureDataCollectEnable)) {
            return R.ok("添加成功");
        } else {
            return R.failed("添加失败");
        }
    }

    @DeleteMapping("/delEnable")
    @SysLog("根据id删除企业采集配置项")
    @ApiOperation(value = "根据id删除企业采集配置项", notes = "根据id企业采集配置项")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    public R removeById(String id) {
        ConfigureDataCollectEnable configureDataCollectEnable = new ConfigureDataCollectEnable();
        if (id == null) {
            return R.failed("企业采集配置id不能为空！");
        } else {
            configureDataCollectEnable.setId(Integer.valueOf(id));
        }
        configureDataCollectEnable.setDelFlag("4");
        boolean flag = configureDataCollectEnableService.removeconfigureDataCollectEnableById(configureDataCollectEnable);
        return flag ? R.ok("删除企业采集配置项成功") : R.failed("删除企业采集配置项失败");
    }

    @PutMapping("/updateEnable")
    @SysLog("修改企业采集配置项")
    @ApiOperation(value = "修改企业采集配置项", notes = "修改企业采集配置项")
    @ApiImplicitParam(name = "configureDataCollectEnable", value = "企业采集配置项对象", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody ConfigureDataCollectEnable configureEnable) {
        String endTime = configureEnable.getEndTime();
        ConfigureDataCollectEnable configureDataCollectEnable = configureDataCollectEnableService.getById(configureEnable.getId());
        if (StringUtils.isEmpty(endTime)) {
            endTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        }
        configureDataCollectEnable.setEndTime(endTime);
        configureDataCollectEnable.setUpdateTime(new Date());
        ScUser user = SecurityUtils.getUser();
        configureDataCollectEnable.setUpdateUserId(user.getId());
        return R.ok(configureDataCollectEnableService.updateById(configureDataCollectEnable));
    }

    @GetMapping("/getEnableDataCodeList")
    @SysLog("获取企业采集配置项")
    @ApiOperation(value = "获取企业采集配置项", notes = "获取企业采集配置项")
    public R getEnableDataCodeList(@RequestParam String enterpriseCode,
                                   @RequestParam String month) {
        return R.ok(configureDataCollectEnableService.getEnableDataCodeList(enterpriseCode,month));
    }

}
