package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.DischargeEnergyAudit;
import com.nantian.cloud.jljc.entity.DischargeEnergyConstruction;
import com.nantian.cloud.jljc.entity.DischargeEnergyPlan;
import com.nantian.cloud.jljc.entity.DischargeEnergyStock;
import com.nantian.cloud.jljc.service.DischargeEnergyAuditService;
import com.nantian.cloud.jljc.service.DischargeEnergyConstructionService;
import com.nantian.cloud.jljc.service.DischargeEnergyPlanService;
import com.nantian.cloud.jljc.service.DischargeEnergyStockService;
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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 两高项目审核流程表服务控制器
 *
 * @author gj
 * @since 2022-02-18 16:38:31
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/dischargeEnergyAudit")
public class DischargeEnergyAuditController {
    private final DischargeEnergyAuditService dischargeEnergyAuditService;
    private final DischargeEnergyStockService dischargeEnergyStockService;
    private final DischargeEnergyPlanService dischargeEnergyPlanService;
    private final DischargeEnergyConstructionService dischargeEnergyConstructionService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @return
     */
    @GetMapping("/page")
    @SysLog("分页查询两高项目审核流程")
    public R getStockPage(Page page, DischargeEnergyAudit dischargeEnergyAudit) {
        dischargeEnergyAudit.setDelFlag("1");  //筛选未删除项
        return R.ok(dischargeEnergyAuditService.getPageData(page,dischargeEnergyAudit));
    }

    /**
     * 查询
     *
     * @param
     * @return
     */
    @GetMapping("/getByItemId")
    @SysLog("查询两高项目审核流程")
    public R getByEpc(DischargeEnergyAudit dischargeEnergyAudit) {
        QueryWrapper<DischargeEnergyAudit> queryWrapper = new QueryWrapper<DischargeEnergyAudit>();
        queryWrapper.eq("item_id",dischargeEnergyAudit.getItemId());
        queryWrapper.eq("item_type",dischargeEnergyAudit.getItemType());
        List<DischargeEnergyAudit> list = dischargeEnergyAuditService.list(queryWrapper);
        return R.ok(list);
    }

    /**
     * 两高项目审核流程添加
     */
    @PostMapping("/save")
    @SysLog("保存两高项目审核流程")
    public R save(@RequestBody DischargeEnergyAudit dischargeEnergyAudit){
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            dischargeEnergyAudit.setUserid(u.getId());
        }
//        if (u.getDeptId() != null) {
//            dischargeEnergyAudit.setDeptId(u.getDeptId());
//        }
        dischargeEnergyAudit.setDelFlag("1");
        dischargeEnergyAudit.setUpdateTime(new Date());
        return R.ok(dischargeEnergyAuditService.save(dischargeEnergyAudit));
    }

    /**
     * 两高项目审核流程修改
     */
    @PostMapping("/edit")
    @SysLog("修改两高项目审核流程")
    public R edit(@RequestBody DischargeEnergyAudit dischargeEnergyAudit){
        if ("1".equals(dischargeEnergyAudit.getProvince())){
            if ("存量".equals(dischargeEnergyAudit.getItemType())){
                DischargeEnergyStock byId = dischargeEnergyStockService.getById(dischargeEnergyAudit.getItemId());
                byId.setState("2");
                dischargeEnergyStockService.updateById(byId);
            }
            if ("在建".equals(dischargeEnergyAudit.getItemType())){
                DischargeEnergyConstruction byId = dischargeEnergyConstructionService.getById(dischargeEnergyAudit.getItemId());
                byId.setState("2");
                dischargeEnergyConstructionService.updateById(byId);
            }
            if ("拟建".equals(dischargeEnergyAudit.getItemType())){
                DischargeEnergyPlan byId = dischargeEnergyPlanService.getById(dischargeEnergyAudit.getItemId());
                byId.setState("2");
                dischargeEnergyPlanService.updateById(byId);
            }
        }
        if ("2".equals(dischargeEnergyAudit.getProvince())&&"2".equals(dischargeEnergyAudit.getCounty())&&"2".equals(dischargeEnergyAudit.getCity())){
            if ("存量".equals(dischargeEnergyAudit.getItemType())){
                DischargeEnergyStock byId = dischargeEnergyStockService.getById(dischargeEnergyAudit.getItemId());
                byId.setState("0");
                dischargeEnergyStockService.updateById(byId);
            }
            if ("在建".equals(dischargeEnergyAudit.getItemType())){
                DischargeEnergyConstruction byId = dischargeEnergyConstructionService.getById(dischargeEnergyAudit.getItemId());
                byId.setState("0");
                dischargeEnergyConstructionService.updateById(byId);
            }
            if ("拟建".equals(dischargeEnergyAudit.getItemType())){
                DischargeEnergyPlan byId = dischargeEnergyPlanService.getById(dischargeEnergyAudit.getItemId());
                byId.setState("0");
                dischargeEnergyPlanService.updateById(byId);
            }
        }
        return R.ok(dischargeEnergyAuditService.updateById(dischargeEnergyAudit));
    }

}