package com.zd.airbottle.service.impl;

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

import com.zd.airbottle.domain.vo.QpUsegasApplyExcelVo;
import com.zd.airbottle.domain.vo.QpUsegasApplyVO;
import com.zd.airbottle.mapper.QpBottleStorageMapper;
import com.zd.common.core.constant.HttpStatus;
import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.template.TemplateResult;
import com.zd.common.core.utils.DateUtils;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.airbottle.mapper.QpUsegasApplyMapper;
import com.zd.airbottle.domain.QpUsegasApply;
import com.zd.airbottle.service.IQpUsegasApplyService;

/**
 * 用气申请Service业务层处理
 *
 * @author zd
 */
@Service
@Slf4j
public class QpUsegasApplyServiceImpl implements IQpUsegasApplyService
{
    @Autowired
    private QpUsegasApplyMapper qpUsegasApplyMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RemoteMessageService remoteMessageService;

    @Autowired
    private QpBottleStorageMapper bottleStorageMapper;

    /**
     * 查询用气申请
     *
     * @param id 用气申请主键
     * @return 用气申请
     */
    @Override
    public QpUsegasApplyVO selectQpUsegasApplyById(Long id)
    {
        QpUsegasApplyVO apply = qpUsegasApplyMapper.selectQpUsegasApplyById(id);
        if(apply.getLeadAuditStaus()==0 || apply.getCenterAuditStatus()==0){
            apply.setRemark("0");//待审核
        }else if(apply.getCenterAuditStatus()==1){
            apply.setRemark("1");//通过
        }else if(apply.getLeadAuditStaus()==2 || apply.getCenterAuditStatus()==2){
            apply.setRemark("2");//驳回
        }else{
            apply.setRemark("");//
        }
        return apply;
    }

    /**
     * 查询用气申请列表
     *
     * @param qpUsegasApply 用气申请
     * @return 用气申请
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u", permi = PerPrefix.AIRBOTTLE_USEAGSAPPLY)
    public List<QpUsegasApplyVO> selectQpUsegasApplyList(QpUsegasApplyVO qpUsegasApply)
    {
        List<QpUsegasApplyVO> list = qpUsegasApplyMapper.selectQpUsegasApplyList(qpUsegasApply);
        if(!list.isEmpty()){
            for (QpUsegasApplyVO qpUsegasApplyVO : list) {
                if (qpUsegasApplyVO.getLeadAuditStaus() == 2 || qpUsegasApplyVO.getCenterAuditStatus() == 2) {
                    qpUsegasApplyVO.setRemark("2");//驳回
                } else if (qpUsegasApplyVO.getLeadAuditStaus() == 0 || qpUsegasApplyVO.getCenterAuditStatus() == 0 || qpUsegasApplyVO.getLeadAuditStaus() == 2) {
                    qpUsegasApplyVO.setRemark("0");//待审核
                } else if (qpUsegasApplyVO.getCenterAuditStatus() == 1) {
                    qpUsegasApplyVO.setRemark("1");//通过
                } else {
                    qpUsegasApplyVO.setRemark("");//
                }
            }
        }
        return list;
    }

    /**
     * 用气管理
     *
     * @param qpUsegasApply 用气申请
     * @return 用气申请集合
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u", permi = PerPrefix.AIRBOTTLE_USEAGSMANAGE)
    public List<QpUsegasApplyVO> selectQpUsegasApplyManageList(QpUsegasApplyVO qpUsegasApply) {
        List<QpUsegasApplyVO> list = qpUsegasApplyMapper.selectQpUsegasApplyList(qpUsegasApply);
        if(!list.isEmpty()){
            for (QpUsegasApplyVO qpUsegasApplyVO : list) {
                if (qpUsegasApplyVO.getLeadAuditStaus() == 2 || qpUsegasApplyVO.getCenterAuditStatus() == 2) {
                    qpUsegasApplyVO.setRemark("2");//驳回
                } else if (qpUsegasApplyVO.getLeadAuditStaus() == 0 || qpUsegasApplyVO.getCenterAuditStatus() == 0 || qpUsegasApplyVO.getLeadAuditStaus() == 2) {
                    qpUsegasApplyVO.setRemark("0");//待审核
                } else if (qpUsegasApplyVO.getCenterAuditStatus() == 1) {
                    qpUsegasApplyVO.setRemark("1");//通过
                } else {
                    qpUsegasApplyVO.setRemark("");//
                }
            }
        }
        return list;
    }

    /**
     * 新增用气申请
     *
     * @param qpUsegasApply 用气申请
     * @return 结果
     */
    @Override
    public int insertQpUsegasApply(QpUsegasApply qpUsegasApply)
    {
        //设置其他公共字段
        SaveUtil.setCommonAttr(qpUsegasApply);

        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        qpUsegasApply.setCreateTime(new Date());//创建时间
        qpUsegasApply.setUserId(sysUser.getUserId());//申请人id
        qpUsegasApply.setApplyUser(sysUser.getNickName());//申请人姓名
        //qpUsegasApply.setPhone(sysUser.getPhonenumber());//电话
        int flg = qpUsegasApplyMapper.insertQpUsegasApply(qpUsegasApply);

        if(flg>0){
            //消息发送
            Map<String,Object> map = bottleStorageMapper.selectSubectDetalsById(qpUsegasApply.getSubjectId());
            if(!map.isEmpty()){
                //userId="申请人id"  checkType = "审核类型 1：资格申请 2：用气申请"), name = "申请人"), applyTime = "申请时间")
                R<TemplateResult> resultR = remoteMessageService.waitCheck(Long.valueOf(map.get("adminId").toString()), 2, sysUser.getNickName(), new Date());
                if (resultR.getCode()!= HttpStatus.SUCCESS){
                    log.error(resultR.getMsg());
                }
            }
        }
        return flg;
    }

    /**
     * 修改用气申请
     *
     * @param qpUsegasApply 用气申请
     * @return 结果
     */
    @Override
    public int updateQpUsegasApply(QpUsegasApply qpUsegasApply)
    {
        return qpUsegasApplyMapper.updateQpUsegasApply(qpUsegasApply);
    }

    /**
     * 批量删除用气申请
     *
     * @param ids 需要删除的用气申请主键
     * @return 结果
     */
    @Override
    public int deleteQpUsegasApplyByIds(Long[] ids)
    {
        return qpUsegasApplyMapper.deleteQpUsegasApplyByIds(ids);
    }

    /**
     * 删除用气申请信息
     *
     * @param id 用气申请主键
     * @return 结果
     */
    @Override
    public int deleteQpUsegasApplyById(Long id)
    {
        return qpUsegasApplyMapper.deleteQpUsegasApplyById(id);
    }

    /***
     * 重新申请
     */
    @Override
    public int updateQpUsegasApplyRepeat(QpUsegasApply qpUsegasApply) {
        return qpUsegasApplyMapper.updateQpUsegasApplyRepeat(qpUsegasApply);
    }

    /**
     * 用气申请导出
     *
     */
    @Override
    public List<QpUsegasApplyExcelVo> qpUsegasApplyExportExcel(QpUsegasApply qpUsegasApply) {
        return qpUsegasApplyMapper.qpUsegasApplyExportExcel(qpUsegasApply);
    }
}
