package com.zd.airbottle.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zd.airbottle.domain.QpFlowDetail;
import com.zd.airbottle.domain.QpUsegasApply;
import com.zd.airbottle.domain.QpUsegasAuditRecord;
import com.zd.airbottle.domain.vo.QpFlowDetailVO;
import com.zd.airbottle.domain.vo.QpUsegasApplyExcelVo;
import com.zd.airbottle.domain.vo.QpUsegasApplyVO;
import com.zd.airbottle.service.IQpFlowDetailService;
import com.zd.airbottle.service.IQpUsegasApplyService;
import com.zd.airbottle.service.IQpUsegasAuditRecordService;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.common.swagger.config.Knife4jConfiguration;
import com.zd.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用气申请Controller
 *
 * @author zd
 * @date 2022-05-10
 */
@RestController
@Api(tags = "【用气申请-管理端】")
//作者手动加入 ，在接口上加作者@ApiOperationSupport(author = "XXX")
@ApiSupport(author = Knife4jConfiguration.Author.CYL)
@RequestMapping("/useAgsApplyManage")
public class QpUsegasApplyManageController extends BaseController
{
    @Autowired
    private IQpUsegasApplyService qpUsegasApplyService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IQpUsegasAuditRecordService qpUsegasAuditRecordService;
    @Autowired
    private IQpFlowDetailService qpFlowDetailService;


    /**
     * 查询用气申请列表
     */
    // @PreAuthorize(hasPermi = "airbottle:useAgsApply:list")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询用气申请列表",notes = "权限字符："+PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.LIST)
    public TableDataInfo<QpUsegasApplyVO> list(QpUsegasApplyVO qpUsegasApply)
    {
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        qpUsegasApply.setUserId(sysUser.getUserId());//当前登陆人申请数据

        //qpUsegasApply.setLeadAuditStaus(1);//实验室负责人审核通过
        //qpUsegasApply.setRemark("100");//按实验室负责人审核时间排序
        startPage();
        List<QpUsegasApplyVO> list = qpUsegasApplyService.selectQpUsegasApplyList(qpUsegasApply);

        return getDataTable(list);
    }

    /**
     * 查询用气管理列表
     */
    // @PreAuthorize(hasPermi = "airbottle:useAgsApply:list")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSMANAGE+ PerFun.LIST)
    @GetMapping("/listManage")
    @ApiOperation(value = "查询用气管理列表",notes = "权限字符："+PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.LIST)
    public TableDataInfo<QpUsegasApplyVO> listManage(QpUsegasApplyVO qpUsegasApply)
    {
        //qpUsegasApply.setLeadAuditStaus(1);//实验室负责人审核通过
        //qpUsegasApply.setRemark("100");//按实验室负责人审核时间排序

        qpUsegasApply.setCenterAuditStatus(1);//通过审核的数据
        startPage();
        List<QpUsegasApplyVO> list = qpUsegasApplyService.selectQpUsegasApplyManageList(qpUsegasApply);

        return getDataTable(list);
    }

    /**
     * 查询用气审核列表
     */
    // @PreAuthorize(hasPermi = "airbottle:useAgsApply:list")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAUDITMANAGE+ PerFun.LIST)
    @GetMapping("/auditList")
    @ApiOperation(value = "查询用气审核列表",notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAUDITMANAGE+ PerFun.LIST)
    public TableDataInfo<QpUsegasApplyVO> auditList(QpUsegasApplyVO qpUsegasApply)
    {
        //qpUsegasApply.setAuditStatus(0);//审核未完成的数据
        startPage();
        List<QpUsegasApplyVO> list = qpUsegasApplyService.selectQpUsegasApplyList(qpUsegasApply);
        return getDataTable(list);
    }

    /**
     * 导出用气申请列表
     */
    @ApiOperation(value = "导出用气申请列表",notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.EXPORT)
    // @PreAuthorize(hasPermi = "airbottle:useAgsApply:export")
    @PreAuthorize(hasPermi =  PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.EXPORT)
    @Log(title = "用气申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QpUsegasApply qpUsegasApply) throws IOException
    {
        //导出审核通过的数据
        List<QpUsegasApplyExcelVo> list = qpUsegasApplyService.qpUsegasApplyExportExcel(qpUsegasApply);
        ExcelUtil<QpUsegasApplyExcelVo> util = new ExcelUtil<QpUsegasApplyExcelVo>(QpUsegasApplyExcelVo.class);
        //本地日期,不包括时分秒
        LocalDate nowDate = LocalDate.now();
        util.exportExcel(response, list, "用气申请数据", null, "用气申请信息导出表"+nowDate);
    }

    /**
     * 获取用气申请详细信息
     */
    @ApiOperation(value = "获取用气申请详细信息",notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.QUERY)
    // @PreAuthorize(hasPermi = "airbottle:useAgsApply:query")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<QpUsegasApply> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(qpUsegasApplyService.selectQpUsegasApplyById(id));
    }

    /**
     * 获取资格申请详细信息
     */
    @ApiOperation(value = "获取资格申请详细信息",notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.QUERY)
    // @PreAuthorize(hasPermi = "airbottle:apply:query")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.QUERY)
    @GetMapping("/getById")
    public AjaxResult getById( Long id)
    {
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        Map<String,Object> map = new HashMap<String,Object>();
        //申请详情
        QpUsegasApplyVO qpUsegasApply =qpUsegasApplyService.selectQpUsegasApplyById(id);
        map.put("apply",qpUsegasApply);

        boolean isAdmin =false;//是否实验室负责人
        boolean audit = false;//是否有审核资格
        //判断当前的登录用户是否该实验室负责人
        if(sysUser.getUserId().equals(qpUsegasApply.getAdminId()) ) {
            isAdmin=true;
            audit=true;
        }
        /*if(0 == qpUsegasApply.getLeadAuditStaus() || 2 == qpUsegasApply.getLeadAuditStaus()){
            if(sysUser.getUserId().equals(qpUsegasApply.getAdminId()) ) {
                audit=true;
            }
        }else */
        if(1 == qpUsegasApply.getLeadAuditStaus()){
            //查询登录用户是否有实验中心审核资格
            QpFlowDetail flowDetail = qpFlowDetailService.selectYqCenterByUserIdIs(sysUser.getUserId());
            if(flowDetail!=null){
                audit=true;
            }
        }
        map.put("isAdmin",isAdmin);
        map.put("audit",audit);

        //审核记录
        QpUsegasAuditRecord record = new QpUsegasAuditRecord();
        record.setUsegasApplyId(id);
        List<QpUsegasAuditRecord> auditlist = qpUsegasAuditRecordService.selectQpUsegasAuditRecordList(record);
         map.put("auditlist",auditlist);

        return AjaxResult.success(map);
    }

    /**
     * 新增用气申请
     */
    @ApiOperation(value = "新增用气申请",notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.ADD)
    //@PreAuthorize(hasPermi = "airbottle:useAgsApply:add")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.ADD)
    @Log(title = "用气申请", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody QpUsegasApply qpUsegasApply)
    {
        return ResultData.result(qpUsegasApplyService.insertQpUsegasApply(qpUsegasApply));
    }

    @ApiOperation(value = "用气重新申请",notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.ADD)
    //@PreAuthorize(hasPermi = "airbottle:useAgsApply:add")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.ADD)
    @Log(title = "用气重新申请", businessType = BusinessType.INSERT)
    @PostMapping("/reapply")
    public ResultData reapply(@RequestBody QpUsegasApply qpUsegasApply)
    {
        if(qpUsegasApply.getId()==null){
            throw new ServiceException("参数为空！");
        }

        //重新录入
        return ResultData.result(qpUsegasApplyService.updateQpUsegasApplyRepeat(qpUsegasApply));
    }

    /**
     * 修改用气申请
     */
    @ApiOperation(value = "修改用气申请" ,notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.EDIT )
    //@PreAuthorize(hasPermi = "airbottle:useAgsApply:edit")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.EDIT)
    @Log(title = "用气申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody QpUsegasApply qpUsegasApply)
    {
        return  ResultData.result(qpUsegasApplyService.updateQpUsegasApply(qpUsegasApply));
    }


    /**
     * 实验中心审核
     */
    @ApiOperation(value = "实验中心审核",notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAPPLY+ "centerexamine")
    //@PreAuthorize(hasPermi = "airbottle:apply:centerexamine")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ "centerexamine")
    @Log(title = "实验中心审核", businessType = BusinessType.UPDATE)
    @PostMapping("/centerExamine")
    public ResultData centerExamine(@RequestBody QpUsegasApply qpUsegasApply)
    {
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();

        //审核表数据录入
        QpUsegasAuditRecord record = new QpUsegasAuditRecord();
        record.setAuditUser(sysUser.getNickName());
        record.setAuditUserid(sysUser.getUserId());
        record.setAuditTime(new Date());
        record.setAuditStatus(qpUsegasApply.getCenterAuditStatus());
        record.setRemark(qpUsegasApply.getCenterAuditCause());
        record.setUsegasApplyId(qpUsegasApply.getId());
        record.setAuditType(2L);
        qpUsegasAuditRecordService.insertQpUsegasAuditRecord(record);

        //审核操作
        qpUsegasApply.setCenterAuditUserid(sysUser.getUserId());
        qpUsegasApply.setCenterAuditUsername(sysUser.getNickName());
        qpUsegasApply.setCenterAuditTime(new Date());
        return  ResultData.result(qpUsegasApplyService.updateQpUsegasApply(qpUsegasApply));
    }

    /**
     * 实验室负责人审核
     */
    @ApiOperation(value = "实验室负责人审核",notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAPPLY+ "leadexamine")
    // @PreAuthorize(hasPermi = "airbottle:apply:leadexamine")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ "leadexamine")
    @Log(title = "实验室负责人审核", businessType = BusinessType.UPDATE)
    @PostMapping("/leadExamine")
    public ResultData leadExamine(@RequestBody QpUsegasApply qpUsegasApply)
    {
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();

        //审核表数据录入
        QpUsegasAuditRecord record = new QpUsegasAuditRecord();
        record.setAuditUser(sysUser.getNickName());
        record.setAuditUserid(sysUser.getUserId());
        record.setAuditTime(new Date());
        record.setAuditStatus(qpUsegasApply.getLeadAuditStaus());
        record.setRemark(qpUsegasApply.getLeadAuditCause());
        record.setUsegasApplyId(qpUsegasApply.getId());
        record.setAuditType(1L);
        qpUsegasAuditRecordService.insertQpUsegasAuditRecord(record);

        //审核操作
        qpUsegasApply.setLeadAuditUserid(sysUser.getUserId());
        qpUsegasApply.setLeadAuditUsername(sysUser.getNickName());
        qpUsegasApply.setLeadAuditTime(new Date());
        return  ResultData.result(qpUsegasApplyService.updateQpUsegasApply(qpUsegasApply));
    }


    /**
     * 删除用气申请
     */
    @ApiOperation(value = "删除用气申请",notes = "权限字符："+ PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.REMOVE)
    // @PreAuthorize(hasPermi = "airbottle:useAgsApply:remove")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.REMOVE)
    @Log(title = "用气申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(qpUsegasApplyService.deleteQpUsegasApplyByIds(ids));
    }
}
