package com.zd.airbottle.controller;

import java.time.LocalDate;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zd.airbottle.domain.*;
import com.zd.airbottle.domain.vo.*;
import com.zd.airbottle.service.IQpFlowDetailService;
import com.zd.airbottle.service.IQpUsegasAuditRecordService;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.airbottle.service.IQpUsegasApplyService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.swagger.config.Knife4jConfiguration;
import com.zd.common.core.web.page.TableDataInfo;

/**
 * 用气申请Controller
 *
 * @author zd
 * @date 2022-05-10
 */
@RestController
@Api(tags = "【用气申请】")
//作者手动加入 ，在接口上加作者@ApiOperationSupport(author = "XXX")
@ApiSupport(author = Knife4jConfiguration.Author.CYL)
@RequestMapping("/useAgsApply")
public class QpUsegasApplyController 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 = "查询用气申请列表")
    public TableDataInfo<QpUsegasApplyVO> list(QpUsegasApplyVO qpUsegasApply)
    {
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        qpUsegasApply.setUserId(sysUser.getUserId());//当前登陆人申请数据

        startPage();
        List<QpUsegasApplyVO> list = qpUsegasApplyService.selectQpUsegasApplyList(qpUsegasApply);
        return getDataTable(list);
    }

    /**
     * 导出用气申请列表
     */
    @ApiOperation(value = "导出用气申请列表")
    // @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 = "获取用气申请详细信息")
    // @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 = "获取资格申请详细信息")
    // @PreAuthorize(hasPermi = "airbottle:apply:query")
    // @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.QUERY)
    @GetMapping("/getById")
    public AjaxResult getById( Long id)
    {
        Map<String,Object> map = new HashMap<String,Object>();
        //申请详情
        QpUsegasApply qpUsegasApply =qpUsegasApplyService.selectQpUsegasApplyById(id);
        map.put("apply",qpUsegasApply);

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

        return AjaxResult.success(map);
    }

    /**
     * 获取用气申请详细信息-根据id查询
     */
    @ApiOperation(value = "获取资格申请详细信息-根据id查询")
    //@PreAuthorize(hasPermi = "airbottle:apply:query")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_QUALIFICATIONAPPLY+ PerFun.QUERY)
    @GetMapping("/getDetails")
    public AjaxResult getDetails( Long id)
    {
        //获取当前登录用户信息
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        Map<String,Object> map = new HashMap<>();

        //申请单详情
        QpUsegasApplyVO usegasApplyVO  =qpUsegasApplyService.selectQpUsegasApplyById(id);
        map.put("apply",usegasApplyVO);

        boolean isAdmin =false;//是否实验室负责人
        boolean audit = false;//是否有审核资格
        //判断当前的登录用户是否该实验室负责人
        if(sysUser.getUserId().equals(usegasApplyVO.getAdminId()) ) {
            isAdmin=true;
        }

        //待审核或者驳回
        if(0 == usegasApplyVO.getLeadAuditStaus() || 2 == usegasApplyVO.getLeadAuditStaus()){
            //查询是否有审核资格
            /*QpFlowDetailVO qpFlowDetail = new QpFlowDetailVO();
            qpFlowDetail.setUserType(1L);//1实验室 2实验中心
            qpFlowDetail.setUserIds(sysUser.getUserId());//登录人id
            qpFlowDetail.setFlowType(2);//用气申请
            List<QpFlowDetailVO> flowDetailList = qpFlowDetailService.selectQpFlowDetailList(qpFlowDetail);
            if(flowDetailList.size()>0){
                audit=true;
            }*/
            if(sysUser.getUserId().equals(usegasApplyVO.getAdminId()) ) {
                audit=true;
            }
        }else if(1 == usegasApplyVO.getLeadAuditStaus()){
            //查询登录用户是否有实验中心审核资格
            QpFlowDetailVO qpFlowDetail = new QpFlowDetailVO();
            qpFlowDetail.setUserType(2L);//1实验室 2实验中心
            qpFlowDetail.setUserId(sysUser.getUserId());//登录人id
            List<QpFlowDetailVO> flowDetailList = qpFlowDetailService.selectQpFlowDetailList(qpFlowDetail);
            if(flowDetailList.size()>0){
                audit=true;
            }
        }
        map.put("isAdmin",isAdmin);
        map.put("audit",audit);


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

        return AjaxResult.success(map);
    }

    /**
     * 新增用气申请
     */
    @ApiOperation(value = "新增用气申请")
    //@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 = "修改用气申请")
    //@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 = "用气重新申请")
    //@PreAuthorize(hasPermi = "airbottle:useAgsApply:add")
    //@PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_USEAGSAPPLY+ PerFun.ADD)
    @Log(title = "用气重新申请", businessType = BusinessType.INSERT)
    @PostMapping("/reapply")
    public ResultData<Boolean> reapply(@RequestBody QpUsegasApply qpUsegasApply)
    {
        if(qpUsegasApply.getId()==null){
            throw new ServiceException("参数为空！");
        }
        return ResultData.result(qpUsegasApplyService.updateQpUsegasApplyRepeat(qpUsegasApply));
    }

    /**
     * 实验中心审核
     */
    @ApiOperation(value = "实验中心审核")
    //@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 = "实验室负责人审核")
    // @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 = "删除用气申请")
    // @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));
    }
}
