package com.yanzhu.jh.trouble.service.impl;

import java.util.*;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DeptTypeEnum;
import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.yanzhu.jh.project.domain.SurProjectUserinfo;
import com.yanzhu.jh.project.mapper.SurProjectUserinfoMapper;
import com.yanzhu.jh.trouble.domain.SmzSspAuditinfo;
import com.yanzhu.jh.trouble.domain.SmzSspFileinfo;
import com.yanzhu.jh.trouble.domain.where.SmzSspProblemmodifyWhere;
import com.yanzhu.jh.trouble.mapper.SmzSspAuditinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.trouble.mapper.SmzSspProblemmodifyMapper;
import com.yanzhu.jh.trouble.domain.SmzSspProblemmodify;
import com.yanzhu.jh.trouble.service.ISmzSspProblemmodifyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 质量整改Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-07-28
 */
@Service
public class SmzSspProblemmodifyServiceImpl implements ISmzSspProblemmodifyService 
{
    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SmzSspAuditinfoMapper smzSspAuditinfoMapper;

    @Autowired
    private SmzSspProblemmodifyMapper smzSspProblemmodifyMapper;

    @Autowired
    private SurProjectUserinfoMapper surProjectUserinfoMapper;

    /**
     * 查询质量整改
     * 
     * @param id 质量整改主键
     * @return 质量整改
     */
    @Override
    public SmzSspProblemmodify selectSmzSspProblemmodifyById(Long id)
    {
        return smzSspProblemmodifyMapper.selectSmzSspProblemmodifyById(id);
    }

    /**
     * 查询质量整改详情
     *
     * @param id 质量整改主键
     * @return 质量整改
     */
    public SmzSspProblemmodify selectSmzSspProblemmodifyInfoById(Long id) {
        return smzSspProblemmodifyMapper.selectSmzSspProblemmodifyInfoById(id);
    }

    /**
     * 查询质量整改列表
     * 
     * @param smzSspProblemmodify 质量整改
     * @return 质量整改
     */
    @Override
    public List<SmzSspProblemmodify> selectSmzSspProblemmodifyList(SmzSspProblemmodify smzSspProblemmodify)
    {
        return smzSspProblemmodifyMapper.selectSmzSspProblemmodifyList(smzSspProblemmodify);
    }

    /**
     * 查询质量整改列表
     *
     * @param smzSspProblemmodify 质量整改
     * @return 质量整改集合
     */
    public List<SmzSspProblemmodify> selectWechatSmzSspProblemmodifyList(SmzSspProblemmodify smzSspProblemmodify) {
        return smzSspProblemmodifyMapper.selectWechatSmzSspProblemmodifyList(smzSspProblemmodify);
    }

    /**
     * 根据状态分组统计数量
     * @param smzSspProblemmodify 查询条件
     * @return List<Map<String, Integer>>
     */
    @Override
    public Map<String, Object> findProblemmodifyGroupByCheckState(SmzSspProblemmodify smzSspProblemmodify) {
        List<Map<String, Object>> dataList = smzSspProblemmodifyMapper.findProblemmodifyGroupByCheckState(smzSspProblemmodify);
        Map<String, Object> dataMap = new HashMap<>();
        if(dataList!=null && dataList.size()>0){
            for(Map<String, Object> map:dataList){
                dataMap.put("check"+map.get("checkState"),map.get("total"));
            }
            dataMap.put("check99",smzSspProblemmodifyMapper.findProblemmodifyTimeoutCount(smzSspProblemmodify));
        }
        return dataMap;
    }

    /**
     * 新增质量整改
     * 
     * @param smzSspProblemmodify 质量整改
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSmzSspProblemmodify(SmzSspProblemmodify smzSspProblemmodify)
    {
        if(smzSspProblemmodify.getSmarkUrl()!=null){
            smzSspProblemmodify.setMarksPicture(smzSspProblemmodify.getSmarkUrl().split(",")[0]);
        }
        smzSspProblemmodify.setCreateTime(DateUtils.getNowDate());
        SysDept sysDept = deptMapper.selectDeptByUserName(smzSspProblemmodify.getCreateUser());
        //当前角色时管理员时，修改角色为集团公司
        String roleType = "";
        if(DeptTypeEnum.JTGS.getCode().equals(sysDept.getTypeFlag())){
            SurProjectUserinfo surProjectUserinfo = new SurProjectUserinfo();
            surProjectUserinfo.setProjectId(smzSspProblemmodify.getProjectId());
            surProjectUserinfo.setCreateBy(smzSspProblemmodify.getCreateUser());
            List<SurProjectUserinfo> datalist = surProjectUserinfoMapper.querySurProjectUserinfoByParams(surProjectUserinfo);
            if(datalist!=null && datalist.size()>0 && datalist.get(0)!=null && PublicStateEnum.USERTYPE_JFJL.getCode().equals(datalist.get(0).getJobType())){
                //说明是甲方经理
                roleType = DeptTypeEnum.JFDBTYPE.getCode();
            }else{
                roleType = DeptTypeEnum.JTGSTYPE.getCode();
            }
        }else if(DeptTypeEnum.ZGS.getCode().equals(sysDept.getTypeFlag())){
            SurProjectUserinfo surProjectUserinfo = new SurProjectUserinfo();
            surProjectUserinfo.setProjectId(smzSspProblemmodify.getProjectId());
            surProjectUserinfo.setCreateBy(smzSspProblemmodify.getCreateUser());
            List<SurProjectUserinfo> datalist = surProjectUserinfoMapper.querySurProjectUserinfoByParams(surProjectUserinfo);
            if(datalist!=null && datalist.size()>0 && datalist.get(0)!=null && PublicStateEnum.USERTYPE_JFJL.getCode().equals(datalist.get(0).getJobType())){
                //说明是甲方经理
                roleType = DeptTypeEnum.JFDBTYPE.getCode();
            }else{
                roleType = DeptTypeEnum.ZGSTYPE.getCode();
            }
        }else if(DeptTypeEnum.JLDW.getCode().equals(sysDept.getTypeFlag())){
            roleType = DeptTypeEnum.JLDWTYPE.getCode();
        }else if(DeptTypeEnum.ZBDW.getCode().equals(sysDept.getTypeFlag())){
            roleType = DeptTypeEnum.ZBDWTYPE.getCode();
        }else if(DeptTypeEnum.FBDW.getCode().equals(sysDept.getTypeFlag())){
            roleType = DeptTypeEnum.FBDWTYPE.getCode();
        }
        smzSspProblemmodify.setRoleType(Convert.toInt(roleType));
        smzSspProblemmodifyMapper.insertSmzSspProblemmodify(smzSspProblemmodify);
        SmzSspAuditinfo smzSspAuditinfo = new SmzSspAuditinfo();
        smzSspAuditinfo.setMainId(smzSspProblemmodify.getId());
        smzSspAuditinfo.setProcessState("1");
        smzSspAuditinfo.setCreateUser(smzSspProblemmodify.getCreateUser());
        smzSspAuditinfo.setCreateTime(new Date());
        int res = smzSspAuditinfoMapper.insertSmzSspAuditinfo(smzSspAuditinfo);
        smzSspAuditinfo.setFileUrls(smzSspProblemmodify.getSmarkUrl());
        insertSmzSspFileinfo(smzSspAuditinfo);
        return res;
    }

    /**
     * 修改质量整改
     * 
     * @param smzSspProblemmodify 质量整改
     * @return 结果
     */
    @Override
    public int updateSmzSspProblemmodify(SmzSspProblemmodify smzSspProblemmodify)
    {
        smzSspProblemmodify.setUpdateTime(DateUtils.getNowDate());
        return smzSspProblemmodifyMapper.updateSmzSspProblemmodify(smzSspProblemmodify);
    }

    /**
     * 批量删除质量整改
     * 
     * @param ids 需要删除的质量整改主键
     * @return 结果
     */
    @Override
    public int deleteSmzSspProblemmodifyByIds(Long[] ids)
    {
        return smzSspProblemmodifyMapper.deleteSmzSspProblemmodifyByIds(ids);
    }

    /**
     * 删除质量整改信息
     * 
     * @param id 质量整改主键
     * @return 结果
     */
    @Override
    public int deleteSmzSspProblemmodifyById(Long id)
    {
        return smzSspProblemmodifyMapper.deleteSmzSspProblemmodifyById(id);
    }

    @Override
    public List<SmzSspProblemmodify> selectSummary(SmzSspProblemmodifyWhere where) {
        return smzSspProblemmodifyMapper.selectSummary(where);
    }

    @Override
    public List<SmzSspProblemmodify> selectSummaryByProject(SmzSspProblemmodifyWhere where) {
        return smzSspProblemmodifyMapper.selectSummaryByProject(where);
    }

    @Override
    public List<SmzSspProblemmodify> countByDate(SmzSspProblemmodifyWhere where) {
        return smzSspProblemmodifyMapper.countByDate(where);
    }

    @Override
    public List<SmzSspProblemmodify> countByDateRange(SmzSspProblemmodifyWhere where) {
        return smzSspProblemmodifyMapper.countByDateRange(where);
    }

    @Override
    public List<SmzSspProblemmodify> groupByInfotypeCheckState(SmzSspProblemmodifyWhere where) {
        return smzSspProblemmodifyMapper.groupByInfotypeCheckState(where);
    }

    @Override
    public Long countTimeout(SmzSspProblemmodifyWhere where) {
        return (long) smzSspProblemmodifyMapper.countTimeout(where);
    }

    /**
     * 新增随手拍文件信息信息
     *
     * @param smzSspAuditinfo 随手拍流程信息对象
     */
    private void insertSmzSspFileinfo(SmzSspAuditinfo smzSspAuditinfo)
    {
        if(smzSspAuditinfo.getFileUrls()!=null){
            String fileUrls[] = smzSspAuditinfo.getFileUrls().split(",");
            Long id = smzSspAuditinfo.getId();
            if (StringUtils.isNotNull(fileUrls))
            {
                List<SmzSspFileinfo> list = new ArrayList<SmzSspFileinfo>();
                for (String fileUrl: fileUrls)
                {
                    SmzSspFileinfo smzSspFileinfo = new SmzSspFileinfo();
                    smzSspFileinfo.setAuditId(id);
                    smzSspFileinfo.setFileType(0L);
                    smzSspFileinfo.setFileUrl(fileUrl);
                    smzSspFileinfo.setIsDel(0L);
                    smzSspFileinfo.setCreateUser(smzSspAuditinfo.getCreateUser());
                    smzSspFileinfo.setCreateTime(new Date());
                    list.add(smzSspFileinfo);
                }
                if (list.size() > 0)
                {
                    smzSspAuditinfoMapper.batchSmzSspFileinfo(list);
                }
            }
        }
    }

}
