package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TReport;
import com.nantian.cloud.jljc.entity.TReportAudit;
import com.nantian.cloud.jljc.service.TReportAuditService;
import com.nantian.cloud.jljc.service.TReportService;
import com.nantian.cloud.jljc.vo.ReportModifyVo;
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 org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 报表-能源报表审批信息
 * @date 2020-09-22
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tReportAudit")
@Api(value = "tReportAudit", tags = "报表-能源报表审批信息")
public class TReportAuditController {

    final private TReportAuditService tReportAuditService;

    final private TReportService tReportService;
   


    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param tReportAudit 能源报表
     * @return
     */
    @GetMapping("/page")
    @SysLog("分页能源报表审批信息")
    @ApiOperation(value = "分页能源报表", notes = "分页能源报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "tReportAudit", value = "报表对象（year 必传）", required = false, dataType = "json", paramType = "path")
    })
    public R getPage(Page page, TReportAudit tReportAudit) {
        tReportAudit.setDelFlag("1"); //筛选未删除项
        return R.ok( tReportAuditService.page(page, Wrappers.query(tReportAudit)));
    }

    

    /**
     * 信息
     *
     * @param tReportAuditId
     * @return R
     */
    @SysLog("通过ID查询报表-能源报表审批信息")
    @ApiOperation(value = "通过ID查询报表-能源报表审批信息",notes = "通过ID查询报表-能源报表审批信息")
    @ApiImplicitParam(name ="tReportAuditId",value = "报表-能源报表审批信息id",required = true,paramType = "path",dataType = "int")
    @GetMapping("/{tReportAuditId}")
    public R getById(@PathVariable("tReportAuditId") Integer tReportAuditId) {
        return R.ok(tReportAuditService.getById(tReportAuditId));
    }

  

    /**
     * 保存
     *
     * @param tReportAudit
     * @return R
     */
    @SysLog("保存报表-能源报表审批信息")
    @ApiOperation(value = "保存报表-能源报表审批信息",notes = "新增报表-能源报表审批信息")
    @ApiImplicitParam(name ="tReportAudit",value = "报表-能源报表审批信息",required = true,dataType = "json",paramType = "path")
    @PostMapping("/save")
    public R save(@RequestBody TReportAudit tReportAudit) {
        if(null==tReportAudit.getReportId()){
            return R.failed("报表Id不能为空！");
        }
        if(StringUtils.isBlank(tReportAudit.getState())){
            return R.failed("审核结果不能为空！");
        }
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            tReportAudit.setUserid(u.getId());
        }
        tReportAudit.setDeptId(u.getDeptId());
        tReportAudit.setDelFlag("1");
        tReportAudit.setUpdateTime(new Date());
        tReportAudit.setCheckUnit(u.getDeptId().toString());
        tReportAudit.setDoPerson(u.getUsername());
        tReportAudit.setCheckTime(tReportAudit.getUpdateTime());
        TReport r= new TReport();
        r.setId(tReportAudit.getReportId().longValue());
        r.setUpdateTime(new Date());
        r.setUpdateTime(tReportAudit.getUpdateTime());
        r.setAuditRemark(tReportAudit.getResult());
        r.setAuditDate(tReportAudit.getUpdateTime());
        r.setAuditor(tReportAudit.getDoPerson());
        r.setAuditorId(u.getId().longValue());
        if("T".equals(tReportAudit.getState())){
            r.setAuditStatus("2");
        }else{
            r.setAuditStatus("4");
        }
        tReportService.updateById(r);
        return R.ok(tReportAuditService.save(tReportAudit));
    }

    /**
     * 修改
     *
     * @param tReportAudit
     * @return R
     */
    @SysLog("修改报表-能源报表审批信息信息")
    @ApiOperation(value = "修改报表-能源报表审批信息信息",notes = "修改报表-能源报表审批信息信息")
    @ApiImplicitParam(name ="tReport",value = "报表-能源报表审批信息",required = true,dataType = "json",paramType = "path")
    @PutMapping
    public R updateById(@RequestBody TReportAudit tReportAudit) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            tReportAudit.setUserid(u.getId());
        }
        tReportAudit.setUpdateTime(new Date());
        return R.ok(tReportAuditService.updateById(tReportAudit));
    }

    /**
     * 删除
     *
     * @param tReportAuditId
     * @return R
     */
    @SysLog("删除报表-能源报表审批信息")
    @ApiOperation(value = "删除报表-能源报表审批信息",notes = "删除报表-能源报表审批信息信息")
    @ApiImplicitParam(name ="tReportId",value = "报表-能源报表审批信息信息ID",required = true,dataType = "int",paramType = "path")
    @DeleteMapping("/{tReportId}")
    public R removeById(@PathVariable("tReportAuditId") Long tReportAuditId) {
        TReportAudit tReportAudit = tReportAuditService.getById(tReportAuditId);
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            tReportAudit.setUserid(u.getId());
        }
        tReportAudit.setDelFlag("4");
        tReportAudit.setUpdateTime(new Date());
        return R.ok(tReportAuditService.updateById(tReportAudit));
    }



}
