package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TProjectEnergySaving;
import com.nantian.cloud.jljc.entity.TProjectEnergySavingAudit;
import com.nantian.cloud.jljc.service.TProjectEnergySavingAuditService;
import com.nantian.cloud.jljc.service.TProjectEnergySavingService;
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.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 固定资产投资项目节能审查-审批意见
 *
 * @date 2020-09-21
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tProjectEnergySavingAudit")
@Api(value = "tProjectEnergySavingAudit", tags = "固定资产投资项目节能审查-审批意见")
public class TProjectEnergySavingAuditController {
    @Autowired
    private TProjectEnergySavingAuditService tProjectEnergySavingAuditService;
    @Autowired
    private TProjectEnergySavingService tProjectEnergySavingService;


    /**
     * 分页获取审批意见
     */
    @GetMapping("/page")
    @SysLog("分页获取固定资产投资项目节能审查审批意见")
    @ApiOperation(value = "分页获取审批意见", notes = "分页获取审批意见")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "tProjectEnergySavingAudit", value = "审批意见对象", required = false, dataType = "json", paramType = "path")
    })
    public R getTProjectEnergySavingAuditPage(Page page, TProjectEnergySavingAudit tProjectEnergySavingAudit) {
        tProjectEnergySavingAudit.setDelFlag("1"); //筛选未删除项
        return R.ok(tProjectEnergySavingAuditService.page(page, Wrappers.query(tProjectEnergySavingAudit)));
    }

    /**
     * 根据id获取审批意见
     */
    @GetMapping("/getById")
    @SysLog("根据id获取固定资产投资项目节能审查审批意见")
    @ApiOperation(value = "根据id获取审批意见", notes = "根据id获取审批意见")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(Long id) {
        return R.ok(tProjectEnergySavingAuditService.getById(id));
    }

    /**
     * 根据固定资产投资项目id获取审批意见
     */
    @GetMapping("/getByProjectId")
    @SysLog("根据固定资产投资项目id获取审批意见")
    @ApiOperation(value = "根据固定资产投资项目id获取审批意见", notes = "根据固定资产投资项目id获取审批意见")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getByProjectId(Long id) {
        TProjectEnergySavingAudit tProjectEnergySavingAudit = new TProjectEnergySavingAudit();
        tProjectEnergySavingAudit.setProjectId(id);
        tProjectEnergySavingAudit.setDelFlag("1");
        return R.ok(tProjectEnergySavingAuditService.list(Wrappers.query(tProjectEnergySavingAudit)));
    }

    /**
     * 根据固定资产投资项目id获取审批意见
     */
    @GetMapping("/getByProjectId2")
    @SysLog("根据固定资产投资项目id获取审批意见")
    @ApiOperation(value = "根据固定资产投资项目id获取审批意见（根据类型返回三个对象）", notes = "根据固定资产投资项目id获取审批意见（根据类型返回三个对象）")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getByProjectId2(Long id) {
        //TProjectEnergySavingAudit tProjectEnergySavingAudit = new TProjectEnergySavingAudit();
        if (id == null) {
            return R.failed("id为空，获取审批意见失败");
        }
        List<TProjectEnergySavingAudit> list1 = tProjectEnergySavingAuditService.getList(id, "1");
        List<TProjectEnergySavingAudit> list2 = tProjectEnergySavingAuditService.getList(id, "2");
        List<TProjectEnergySavingAudit> list3 = tProjectEnergySavingAuditService.getList(id, "3");
        Map<String, Object> map = new HashMap<>();
        map.put("type1", list1);
        map.put("type2", list2);
        map.put("type3", list3);
        log.error("根据固定资产投资项目id获取审批意见：{}", map);
        return R.ok(map);
    }

    /**
     * 更新政府备案、审批、核准的审批意见
     */
    @PostMapping("/audit")
    @SysLog("更新政府备案、审批、核准的审批意见")
    @ApiOperation(value = "更新政府备案、审批、核准的审批意见", notes = "更新政府备案、审批、核准的审批意见")
    @ApiImplicitParam(name = "tProjectEnergySavingAudit", value = "审批意见对象（类型(处置方式)：1 备案 2 审批 3 核准）", required = false, dataType = "json", paramType = "path")
    public R audit(@RequestBody TProjectEnergySavingAudit tProjectEnergySavingAudit) {
        log.error("更新政府初审、机构评审、备案项目的审批意见：{}", tProjectEnergySavingAudit);
        ScUser u = SecurityUtils.getUser();

        if (tProjectEnergySavingAudit.getType() == null || "".equals(tProjectEnergySavingAudit.getType())) {
            return R.failed("类型type不能为空！");
        }

        //固定资产项目
        TProjectEnergySaving project = tProjectEnergySavingService.getById(tProjectEnergySavingAudit.getProjectId());
        if (tProjectEnergySavingAudit.getProjectId() == null || project == null) {
            if ("1".equals(tProjectEnergySavingAudit.getType())) {
                return R.failed("固定资产投资项目不存在，项目备案失败！");
            } else if ("2".equals(tProjectEnergySavingAudit.getType())) {
                return R.failed("固定资产投资项目不存在，项目审批失败！");
            } else if ("3".equals(tProjectEnergySavingAudit.getType())) {
                return R.failed("固定资产投资项目不存在，项目核准失败！");
            } else {
            }
        }

        //审批意见
        tProjectEnergySavingAudit.setDelFlag("1");
        tProjectEnergySavingAudit.setDeptId(u.getDeptId());
        tProjectEnergySavingAudit.setUpdateTime(new Date());
        tProjectEnergySavingAudit.setUserid(u.getId());
        tProjectEnergySavingAudit.setCheckTime(new Date());
        tProjectEnergySavingAudit.setType(project.getAction());
        boolean b = tProjectEnergySavingAuditService.saveOrUpdate(tProjectEnergySavingAudit);

        //审核状态为通过，修改项目表状态
        boolean c = false;
        if ("T".equals(tProjectEnergySavingAudit.getState())) {
            TProjectEnergySaving tProjectEnergySaving = new TProjectEnergySaving();
            tProjectEnergySaving.setId(tProjectEnergySavingAudit.getProjectId());
            tProjectEnergySaving.setUpdateTime(new Date());
            tProjectEnergySaving.setUserid(u.getId());
            //tProjectEnergySaving.setState(tProjectEnergySavingAudit.getType());
            tProjectEnergySaving.setState("2"); //政府端已审核
            c = tProjectEnergySavingService.updateById(tProjectEnergySaving);
        }

        return R.ok(b && c);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @SysLog("删除审批意见")
    @ApiOperation(value = "删除固定资产投资项目审批意见", notes = "删除审批意见")
    @ApiImplicitParam(name = "TProjectEnergySavingAuditId", value = "id", required = true, dataType = "int", paramType = "path")
    public R removeById(Integer id) {
        TProjectEnergySavingAudit tProjectEnergySavingAudit = tProjectEnergySavingAuditService.getById(id);
        if (tProjectEnergySavingAudit == null) {
            return R.failed("审批意见不存在，删除失败");
        }
        ScUser u = SecurityUtils.getUser();
        tProjectEnergySavingAudit.setDelFlag("4");
        tProjectEnergySavingAudit.setUserid(u.getId());
        tProjectEnergySavingAudit.setUpdateTime(new Date());
        return R.ok(tProjectEnergySavingAuditService.updateById(tProjectEnergySavingAudit));
    }

}
