package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TDualcontrolProgress;
import com.nantian.cloud.jljc.entity.TDualcontrolProgressApply;
import com.nantian.cloud.jljc.service.TDualcontrolProgressApplyService;
import com.nantian.cloud.jljc.service.TDualcontrolProgressService;
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.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 能耗双控-完成情况修改申请
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tDualcontrolProgressApply")
@Api(value="tDualcontrolProgressApply",tags = "能耗双控-完成情况修改申请表")
public class TDualcontrolProgressApplyController {

    @Autowired
    private TDualcontrolProgressApplyService applyService;
    @Autowired
    private TDualcontrolProgressService progressService;

    /**
     * 省查看州市申请，州市查看区县申请
     */
    @GetMapping("page")
    @SysLog("完成情况修改申请记录查询（双控）")
    @ApiOperation(value = "完成情况修改申请记录查询", notes = "完成情况修改申请记录查询")
    public R page(Page page, TDualcontrolProgressApply apply){
        return R.ok(applyService.pageByScope(page,apply));
    }

    @GetMapping("count")
    @SysLog("未审核的完成情况修改申请条数（双控）")
    @ApiOperation(value = "未审核的完成情况修改申请条数", notes = "未审核的完成情况修改申请条数")
    public R count(TDualcontrolProgressApply apply){
        return R.ok(applyService.countByScope(apply));
    }

    @GetMapping("ownApply")
    @SysLog("本部门申请修改完成情况记录（双控）")
    @ApiOperation(value = "本部门申请修改完成情况记录", notes = "本部门申请修改完成情况记录")
    public R ownApply(Page page, TDualcontrolProgressApply apply){
        return R.ok(applyService.ownApply(page,apply));
    }

    @PostMapping("apply")
    @SysLog("完成情况修改申请（双控）")
    @ApiOperation(value = "完成情况修改申请", notes = "完成情况修改申请")
    public R apply(@RequestBody TDualcontrolProgressApply apply){
        ScUser u = SecurityUtils.getUser();
        apply.setModifyTime(new Date());
        apply.setModifyUserId(u.getId());
        apply.setModifyUserName(u.getUserRealName());
        return R.ok(applyService.save(apply));
    }

    @PostMapping("audit")
    @SysLog("完成情况修改审批（双控）")
    @ApiOperation(value = "完成情况修改审批", notes = "完成情况修改审批")
    public R audit(@RequestBody TDualcontrolProgressApply apply){
        ScUser u = SecurityUtils.getUser();
        apply.setAuditTime(new Date());
        apply.setAuditUserId(u.getId());
        apply.setAuditUserName(u.getUserRealName());
        applyService.updateById(apply);
        if(apply.getAuditResult().equals("1")){ //审核通过，完成情况可进行修改
            TDualcontrolProgress progress = new TDualcontrolProgress();
            progress.setId(apply.getProgressId());
            progress.setItemStatus("1");
            progressService.updateById(progress);
        }
        return R.ok();
    }

    @PostMapping("/retreat")
    @SysLog("完成情况退回（双控）")
    @ApiOperation(value = "完成情况退回",notes = "完成情况退回")
    public R retreat(@RequestBody TDualcontrolProgressApply apply){
        TDualcontrolProgress progress = new TDualcontrolProgress();
        progress.setId(apply.getProgressId());
        progress.setItemStatus("4"); //退回
        progressService.updateById(progress);
        ScUser u = SecurityUtils.getUser();
        apply.setAuditTime(new Date());
        apply.setAuditUserId(u.getId());
        apply.setAuditUserName(u.getUserRealName());
        applyService.save(apply);
        return R.ok();
    }
}
