package com.zd.airbottle.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.zd.airbottle.domain.vo.QpQualificationApplyDetailVO;
import com.zd.airbottle.domain.vo.QpQualificationApplyVO;
import com.zd.airbottle.mapper.QpBottleStorageMapper;
import com.zd.airbottle.mapper.QpQualificationApplyDetailMapper;
import com.zd.airbottle.mapper.QpSupplierMapper;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.service.TokenService;
import com.zd.common.security.utils.SaveUtil;

import com.zd.system.api.domain.SysUser;
import com.zd.system.api.message.RemoteMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.airbottle.mapper.QpQualificationApplyMapper;
import com.zd.airbottle.domain.QpQualificationApply;
import com.zd.airbottle.service.IQpQualificationApplyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用气资格申请Service业务层处理
 *
 * @author zd
 * @date 2022-05-10
 */
@Service
public class QpQualificationApplyServiceImpl implements IQpQualificationApplyService
{
    @Autowired
    private QpQualificationApplyMapper qpQualificationApplyMapper;

    @Autowired
    private QpQualificationApplyDetailMapper qpQualificationApplyDetailMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private QpSupplierMapper supplierMapper;

    @Autowired
    private QpBottleStorageMapper bottleStorageMapper;

    @Autowired
    private RemoteMessageService remoteMessageService;

    /**
     * 查询资格申请
     *
     * @param id 资格申请主键
     * @return 资格申请
     */
    @Override
    public QpQualificationApplyVO selectQpQualificationApplyById(Long id)
    {
        QpQualificationApplyVO apply = qpQualificationApplyMapper.selectQpQualificationApplyById(id);
        if(apply.getLeadAuditStatus()==0 || apply.getCenterAuditStatus()==0){
            apply.setRemark("0");//待审核
        }else if(apply.getCenterAuditStatus()==1){
            apply.setRemark("1");//通过
        }else if(apply.getLeadAuditStatus()==2 || apply.getCenterAuditStatus()==2){
            apply.setRemark("2");//驳回
        }else{
            apply.setRemark("");//
        }

        return apply;
    }

    /**
     * 查询资格申请列表
     *
     * @param qpQualificationApply 资格申请
     * @return 资格申请
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u", permi = PerPrefix.AIRBOTTLE_QUALIFICATIONAPPLYMANAGE)
    public List<QpQualificationApplyVO> selectQpQualificationApplyList(QpQualificationApplyVO qpQualificationApply)
    {
        List<QpQualificationApplyVO> list= qpQualificationApplyMapper.selectQpQualificationApplyList(qpQualificationApply);
        if(!list.isEmpty()){
            for (int i=0;i<list.size();i++){
                if(list.get(i).getLeadAuditStatus()==2 || list.get(i).getCenterAuditStatus()==2){
                    list.get(i).setRemark("2");//驳回
                }else if(list.get(i).getCenterAuditStatus()==1){
                    list.get(i).setRemark("1");//通过
                }else   if(list.get(i).getLeadAuditStatus()==0 || list.get(i).getCenterAuditStatus()==0){
                    list.get(i).setRemark("0");//待审核
                } else{
                    list.get(i).setRemark("");//
                }
            }
        }
        return list;
    }

    /****
     * 资格管理列表
     * @param qpQualificationApply
     * @return
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u", permi = PerPrefix.AIRBOTTLE_QUALIFICATIONMANAGE)
    public List<QpQualificationApplyVO> selectQpQualificationApplyManageList(QpQualificationApplyVO qpQualificationApply) {
        List<QpQualificationApplyVO> list= qpQualificationApplyMapper.selectQpQualificationApplyList(qpQualificationApply);
        if(!list.isEmpty()){
            for (int i=0;i<list.size();i++){
                if(list.get(i).getLeadAuditStatus()==2 || list.get(i).getCenterAuditStatus()==2){
                    list.get(i).setRemark("2");//驳回
                }else if(list.get(i).getCenterAuditStatus()==1){
                    list.get(i).setRemark("1");//通过
                }else   if(list.get(i).getLeadAuditStatus()==0 || list.get(i).getCenterAuditStatus()==0){
                    list.get(i).setRemark("0");//待审核
                } else{
                    list.get(i).setRemark("");//
                }
            }
        }
        return list;
    }

    /**
     * 新增资格申请
     *
     * @param qpQualificationApplyVo 资格申请
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertQpQualificationApply(QpQualificationApplyVO qpQualificationApplyVo)
    {
        //设置其他公共字段
        SaveUtil.setCommonAttr(qpQualificationApplyVo);

        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        int flg=0;

        //申请表数据录入
        qpQualificationApplyVo.setUserId(sysUser.getUserId());
        qpQualificationApplyVo.setApplyUser(sysUser.getNickName());
        flg= qpQualificationApplyMapper.insertQpQualificationApply(qpQualificationApplyVo);

        List<QpQualificationApplyDetailVO> listdetailVo = qpQualificationApplyVo.getListDetailVo();
        //详情表数据录入
        if(listdetailVo!=null && listdetailVo.size()>0) {
            for (int i = 0; i < listdetailVo.size(); i++) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("airName",listdetailVo.get(i).getAirName());//气体名称
                map.put("gasLevel",listdetailVo.get(i).getConfigName());//规格
                map.put("companyName",listdetailVo.get(i).getCompanyName());//商家名称
                map.put("airConstituents",listdetailVo.get(i).getAirConstituents());//气体组分
                listdetailVo.get(i).setApplyDescribe(JSON.toJSONString(map));
                listdetailVo.get(i).setQualificateApplyId(qpQualificationApplyVo.getId());//申请id
                qpQualificationApplyDetailMapper.insertQpQualificationApplyDetail(listdetailVo.get(i));
            }
        }
        if(flg>0){
            //消息发送
           Map<String,Object> map = bottleStorageMapper.selectSubectDetalsById(qpQualificationApplyVo.getSubjectId());
            if(!map.isEmpty()){
                //userId="申请人id"  checkType = "审核类型 1：资格申请 2：用气申请"), name = "申请人"), applyTime = "申请时间")
                remoteMessageService.waitCheck(Long.valueOf(map.get("adminId").toString()),1,sysUser.getNickName(),new Date());
            }
        }
        return flg;
    }

    /**
     * 修改资格申请
     *
     * @param qpQualificationApply 资格申请
     * @return 结果
     */
    @Override
    public int updateQpQualificationApply(QpQualificationApply qpQualificationApply)
    {
        return qpQualificationApplyMapper.updateQpQualificationApply(qpQualificationApply);
    }

    /**
     * 批量删除资格申请
     *
     * @param ids 需要删除的资格申请主键
     * @return 结果
     */
    @Override
    public int deleteQpQualificationApplyByIds(Long[] ids)
    {
        return qpQualificationApplyMapper.deleteQpQualificationApplyByIds(ids);
    }

    /**
     * 删除资格申请信息
     *
     * @param id 资格申请主键
     * @return 结果
     */
    @Override
    public int deleteQpQualificationApplyById(Long id)
    {
        return qpQualificationApplyMapper.deleteQpQualificationApplyById(id);
    }

    /**
     * 资格申请 和用气申请合并查询
     *
     * @param applyVO
     * @return
     */
    @Override
    public List<QpQualificationApplyVO> selectQpApplyList(QpQualificationApplyVO applyVO) {
        return qpQualificationApplyMapper.selectQpApplyList(applyVO);
    }

    /***
     * 小程序-管理端-资格审核
     * @param applyVO
     * @return
     */
    @Override
    public List<QpQualificationApplyVO> selectQpApplyListManageApp(QpQualificationApplyVO applyVO) {
        List<QpQualificationApplyVO> list = qpQualificationApplyMapper.selectQpApplyListManageApp(applyVO);
        return list;
    }

    /***
     * 重新申请
     * @param qualificationApply
     */
    @Override
    public int updateQpQualificationApplyRepeat(QpQualificationApply qualificationApply) {
        if(StringUtils.isNull(qualificationApply.getId())){
            throw  new ServiceException("参数为空！");
        }
        return qpQualificationApplyMapper.updateQpQualificationApplyRepeat(qualificationApply);
    }

    /***
     * 根据当前登录用户 查询资格申请总数
     * @param applyVO
     * @return
     */
    @Override
    public List<QpQualificationApplyVO> selectApplyListCount(QpQualificationApplyVO applyVO) {
        return qpQualificationApplyMapper.selectApplyListCount(applyVO);
    }

    /**
     * 查询申请的实验室是否有准入资格
     *
     * @param map
     * @return
     */
    @Override
    public String checkStudentQualification(Map<String, Object> map) {
        return qpQualificationApplyMapper.checkStudentQualification(map);
    }

    /**
     * 老师是否有准入资格
     * @param map
     * @return
     */
    public String checkTeacherQualification(Map<String, Object> map){
        return qpQualificationApplyMapper.checkTeacherQualification(map);
    }

    /**
     * 我的实验室集合
     *
     * @param adminId
     * @return
     */
    @Override
    public List<Map<String, Object>> selectMySubject(Long adminId) {
        return qpQualificationApplyMapper.selectMySubject(adminId);
    }
}
