package com.nantian.cloud.jly.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.jly.entity.CheckEnterprice;
import com.nantian.cloud.jly.entity.CheckPlan;
import com.nantian.cloud.jly.service.CheckPlanService;
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.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 审查计划表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/checkPlan")
@Api(value = "checkPlan", tags = "审查计划表")
public class CheckPlanController {
    private final CheckPlanService checkPlanService;


    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param checkPlan 审查计划查询
     * @return
     */
    @SysLog("审查计划表-分页查询")
    @GetMapping("/page")
    @ApiOperation(value = "查询审查计划表", notes = "条件查询审查计划表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "CheckPlan", value = "审查计划表（查询条件：审查计划name、审查方法checkMethod(1资料审查,2资料及现场审查)、" +
                    "审查类型checkType(1管理审查,2单位自查)、状态进度checkStatus(1未提交资料,2在线审查,3线下抽查)）", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })
    public R getcheckPlanPage(Page page, CheckPlan checkPlan) {
        checkPlan.setDelFlag("1"); //查询未删除数据
        return R.ok(checkPlanService.getByPage(page,checkPlan));
    }


    /**
     * 信息
     *
     * @param checkPlanId
     * @return R
     */
    @SysLog("审查计划表-查询审查计划表")
    @GetMapping("/{checkPlanId}")
    @ApiOperation(value = "查询审查计划表", notes = "条件查询审查计划表")
    @ApiImplicitParam(name = "checkPlanId", value = "审查计划表id", required = true, dataType = "int", paramType = "path")
    public R getById(@PathVariable("checkPlanId") Integer checkPlanId) {
        return R.ok(checkPlanService.getById(checkPlanId));
    }

    /**
     * 通过企业编码分页查询审查计划表
     */
    @SysLog("审查计划表-通过企业编码分页查询审查计划表")
    @GetMapping("/getPageByEpc")
    @ApiOperation(value = "通过企业编码分页查询审查计划表", notes = "通过企业编码分页查询审查计划表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "epc", value = "企业编码", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })
    public R getPageByEpc(Page page,String epc) {
        IPage<CheckPlan> planPage = checkPlanService.getPageByEpc(page, epc);
        return R.ok(planPage);
    }


    /**
     * 保存
     *
     * @param checkPlan
     * @return R
     */
    @SysLog("审查计划表-保存")
    @PostMapping
    @ApiOperation(value = "保存审查计划表", notes = "新增审查计划表")
    @ApiImplicitParam(name = "CheckPlan", value = "审查计划表", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody CheckPlan checkPlan) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkPlan.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            checkPlan.setDeptId(u.getDeptId());
        }
        checkPlan.setDelFlag("1");
        checkPlan.setUpdateTime(new Date());
        return R.ok(checkPlanService.save(checkPlan));
    }

    /**
     * 保存
     *
     * @param checkPlan
     * @return R
     */
    @SysLog("审查计划表-保存")
    @PostMapping("sendPlan")
    @ApiOperation(value = "政府端下发计划", notes = "政府端下发计划")
    @ApiImplicitParam(name = "CheckPlan", value = "审查计划表", required = true, dataType = "json", paramType = "path")
    public R sendPlan(@RequestBody CheckPlan checkPlan) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkPlan.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            checkPlan.setDeptId(u.getDeptId());
        }
        checkPlan.setDelFlag("1");
        checkPlan.setUpdateTime(new Date());
        return R.ok(checkPlanService.save(checkPlan));
    }

    /**
     * 修改
     *
     * @param checkPlan
     * @return R
     */
    @SysLog("审查计划表-修改")
    @PutMapping
    @ApiOperation(value = "修改审查计划表", notes = "修改审查计划表")
    @ApiImplicitParam(name = "CheckPlan", value = "审查计划表", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody CheckPlan checkPlan) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkPlan.setUserid(u.getId());
        }
        checkPlan.setUpdateTime(new Date());
        return R.ok(checkPlanService.updateById(checkPlan));
    }

    /**
     * 删除
     *
     * @param checkPlanId
     * @return R
     */
    @SysLog("审查计划表-删除")
    @DeleteMapping("/{checkPlanId}")
    @ApiOperation(value = "删除审查计划表", notes = "删除审查计划表")
    @ApiImplicitParam(name = "checkPlanId", value = "审查计划表id", required = true, dataType = "int", paramType = "path")
    public R removeById(@PathVariable("checkPlanId") Long checkPlanId) {
        CheckPlan checkPlan = checkPlanService.getById(checkPlanId);
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkPlan.setUserid(u.getId());
        }
        checkPlan.setDelFlag("4");
        checkPlan.setUpdateTime(new Date());
        return R.ok(checkPlanService.updateById(checkPlan));
    }

}
