package com.zcloud.service.inspection.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.inspection.SafetyEnvironmentalInspectorMapper;
import com.zcloud.service.bus.NoticeCorpUtil;
import com.zcloud.service.inspection.SafetyEnvironmentalInspectorService;
import com.zcloud.service.inspection.SafetyEnvironmentalLogService;
import com.zcloud.service.inspection.SafetyEnvironmentalService;
import com.zcloud.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 说明：安全环保检查人员
 * 作者：luoxiaobao
 * 时间：2022-06-08
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class SafetyEnvironmentalInspectorServiceImpl implements SafetyEnvironmentalInspectorService {

    @Resource
    private SafetyEnvironmentalInspectorMapper safetyenvironmentalinspectorMapper;
    @Autowired
    private SafetyEnvironmentalService safetyEnvironmentalService;
    @Autowired
    private SafetyEnvironmentalLogService safetyenvironmentallogService;
    @Resource
    private NoticeCorpUtil util;

    @Override
    public void verify(PageData pd) throws Exception {
        if ("1".equals(pd.getString("INSPECTION_STATUS"))) {    //检查进度状态 1.部分检查人员已核实
            List<PageData> list = safetyenvironmentalinspectorMapper.listOtherNotVerify(pd);
            if (list.size() == 0) {
                PageData inspector = safetyEnvironmentalService.findById(pd);
                pd.put("inspector",inspector.getString("INSPECTED_SITEUSER_ID"));
                pd.put("INSPECTION_STATUS", "2");    //检查进度状态 2.所有检查人员已核实，待被检查人确认
                util.SE_TwoStepUtil(pd);
            }
        }
        safetyEnvironmentalService.editStatus(pd);
        if ("-1".equals(pd.getString("INSPECTION_STATUS")) || "-2".equals(pd.getString("INSPECTION_STATUS"))) {
            util.SE_returnOneStepUtil(pd);
        }

        PageData log = new PageData();
        log.put("INSPECTION_LOG_ID", UuidUtil.get32UUID());    //安全环保检查操作记录ID
        log.put("INSPECTION_ID", pd.getString("INSPECTION_ID"));    //安全环保检查ID
        log.put("ACTION", "安全环保检查申请");    //操作内容
        log.put("CREATTIME", pd.getString("OPERATTIME"));    //添加时间
        log.put("CREATOR", pd.getString("ACTION_USER"));    //添加人姓名
        safetyenvironmentallogService.save(log);
        safetyenvironmentalinspectorMapper.edit(pd);
    }

    /**
     * 新增
     *
     * @param pd
     * @throws Exception
     */
    public void save(PageData pd) throws Exception {
        safetyenvironmentalinspectorMapper.save(pd);
    }

    /**
     * 删除
     *
     * @param pd
     * @throws Exception
     */
    public void delete(PageData pd) throws Exception {
        safetyenvironmentalinspectorMapper.delete(pd);
    }

    /**
     * 修改
     *
     * @param pd
     * @throws Exception
     */
    public void edit(PageData pd) throws Exception {
        safetyenvironmentalinspectorMapper.edit(pd);
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    public List<PageData> list(Page page) throws Exception {
        return safetyenvironmentalinspectorMapper.datalistPage(page);
    }

    /**
     * 列表(全部)
     *
     * @param pd
     * @throws Exception
     */
    public List<PageData> listAll(PageData pd) throws Exception {
        return safetyenvironmentalinspectorMapper.listAll(pd);
    }

    /**
     * 通过id获取数据
     *
     * @param pd
     * @throws Exception
     */
    public PageData findById(PageData pd) throws Exception {
        return safetyenvironmentalinspectorMapper.findById(pd);
    }

    /**
     * 通过InspectionId获取数据
     *
     * @param pd
     * @throws Exception
     */
    public PageData findByInspectionId(PageData pd) throws Exception {
        return safetyenvironmentalinspectorMapper.findByInspectionId(pd);
    }

    /**
     * 批量删除
     *
     * @param pd
     * @throws Exception
     */
    public void deleteAll(PageData pd) throws Exception {
        safetyenvironmentalinspectorMapper.deleteAll(pd);
    }

    /**
     * 列表(根据所选ID获取数据)
     *
     * @param pd
     * @throws Exception
     */
    public List<PageData> findByIds(PageData pd) throws Exception {
        return safetyenvironmentalinspectorMapper.findByIds(pd);
    }

    /**
     * 删除（根据检查ID）
     *
     * @param pd
     * @throws Exception
     */
    @Override
    public void deleteByInspection(PageData pd) throws Exception {
        safetyenvironmentalinspectorMapper.deleteByInspection(pd);
    }

    /**
     * 查询检查人确认数
     *
     * @param pd
     * @throws Exception
     */
    public PageData confirmCount(PageData pd) throws Exception {
        return safetyenvironmentalinspectorMapper.confirmCount(pd);
    }

    @Override
    public PageData countHome(PageData pd) throws Exception {
        return safetyenvironmentalinspectorMapper.countHome(pd);
    }
}

