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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

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.SurProject;
import com.yanzhu.jh.project.domain.SurProjectUserinfo;
import com.yanzhu.jh.project.mapper.SurProjectMapper;
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.SmzSspProblemmodify;
import com.yanzhu.jh.trouble.mapper.SmzSspAuditinfoMapper;
import com.yanzhu.jh.trouble.mapper.SmzSspProblemmodifyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.trouble.mapper.SmzSspProblemmodifyDraftMapper;
import com.yanzhu.jh.trouble.domain.SmzSspProblemmodifyDraft;
import com.yanzhu.jh.trouble.service.ISmzSspProblemmodifyDraftService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 随手拍问题整改质量留痕信息Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-10-15
 */
@Service
public class SmzSspProblemmodifyDraftServiceImpl implements ISmzSspProblemmodifyDraftService 
{
    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SurProjectMapper surProjectMapper;

    @Autowired
    private SmzSspAuditinfoMapper smzSspAuditinfoMapper;

    @Autowired
    private SmzSspProblemmodifyMapper smzSspProblemmodifyMapper;

    @Autowired
    private SurProjectUserinfoMapper surProjectUserinfoMapper;

    @Autowired
    private SmzSspProblemmodifyDraftMapper smzSspProblemmodifyDraftMapper;

    /**
     * 查询随手拍问题整改质量留痕信息
     * 
     * @param id 随手拍问题整改质量留痕信息主键
     * @return 随手拍问题整改质量留痕信息
     */
    @Override
    public SmzSspProblemmodifyDraft selectSmzSspProblemmodifyDraftById(Long id)
    {
        return smzSspProblemmodifyDraftMapper.selectSmzSspProblemmodifyDraftById(id);
    }

    /**
     * 查询随手拍问题整改质量留痕信息列表
     * 
     * @param smzSspProblemmodifyDraft 随手拍问题整改质量留痕信息
     * @return 随手拍问题整改质量留痕信息
     */
    @Override
    public List<SmzSspProblemmodifyDraft> selectSmzSspProblemmodifyDraftList(SmzSspProblemmodifyDraft smzSspProblemmodifyDraft)
    {
        return smzSspProblemmodifyDraftMapper.selectSmzSspProblemmodifyDraftList(smzSspProblemmodifyDraft);
    }

    /**
     * 新增随手拍问题整改质量留痕信息
     * 
     * @param smzSspProblemmodifyDraft 随手拍问题整改质量留痕信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSmzSspProblemmodifyDraft(SmzSspProblemmodifyDraft smzSspProblemmodifyDraft)
    {
        //当前角色时管理员时，修改角色为集团公司
        String roleType = getRoleType(smzSspProblemmodifyDraft.getProjectId(),smzSspProblemmodifyDraft.getCreateUser());
        AtomicInteger count = new AtomicInteger();
        for(List<String> urls:smzSspProblemmodifyDraft.getFileUrls()){
            SmzSspProblemmodifyDraft insert = new SmzSspProblemmodifyDraft();
            insert.setProjectId(smzSspProblemmodifyDraft.getProjectId());
            insert.setInfoType(smzSspProblemmodifyDraft.getInfoType());
            //兼容项目名称为空的异常...
            if(StringUtils.isEmpty(smzSspProblemmodifyDraft.getProblemArea())){
                SurProject surProject = surProjectMapper.selectSurProjectById(smzSspProblemmodifyDraft.getProjectId());
                insert.setProblemArea(surProject.getProjectName());
            }else{
                insert.setProblemArea(smzSspProblemmodifyDraft.getProblemArea());
            }
            insert.setLordSent(smzSspProblemmodifyDraft.getLordSent());
            insert.setLordSentUser(smzSspProblemmodifyDraft.getLordSentUser());
            insert.setCheckState(smzSspProblemmodifyDraft.getCheckState());
            insert.setCreateUser(smzSspProblemmodifyDraft.getCreateUser());
            insert.setCreateTime(new Date());
            insert.setSmarkUrl(String.join(",", urls));
            insert.setMarksPicture(urls.get(0));
            insert.setProblemType("1");
            insert.setRoleType(Convert.toLong(roleType));
            count.addAndGet(smzSspProblemmodifyDraftMapper.insertSmzSspProblemmodifyDraft(insert));
        }
        return count.intValue();
    }

    /**
     * 批量修改随手拍问题整改质量留痕信息
     *
     * @param smzSspProblemmodifyList 随手拍问题整改质量留痕信息
     * @return 结果
     */
    @Override
    @Transactional
    public int batchUpdateSmzSspProblemmodifyDraft(List<SmzSspProblemmodify> smzSspProblemmodifyList) {
        int res = 0;
        for(SmzSspProblemmodify smzSspProblemmodify:smzSspProblemmodifyList){
            if(smzSspProblemmodify.getId()!=null){
                //删除草稿
                smzSspProblemmodifyDraftMapper.deleteSmzSspProblemmodifyDraftById(smzSspProblemmodify.getId());
            }
            if(smzSspProblemmodify.getRoleType()==null || smzSspProblemmodify.getRoleType()<1){
                smzSspProblemmodify.setRoleType(Convert.toInt(getRoleType(smzSspProblemmodify.getProjectId(),smzSspProblemmodify.getCreateUser())));
            }
            //设置主图
            smzSspProblemmodify.setMarksPicture(smzSspProblemmodify.getSmarkUrl().split(",")[0]);
            //兼容项目名称为空的异常...
            if(StringUtils.isEmpty(smzSspProblemmodify.getProblemArea())){
                SurProject surProject = surProjectMapper.selectSurProjectById(smzSspProblemmodify.getProjectId());
                smzSspProblemmodify.setProblemArea(surProject.getProjectName());
            }
            smzSspProblemmodifyMapper.insertSmzSspProblemmodify(smzSspProblemmodify);
            SmzSspAuditinfo smzSspAuditinfo = new SmzSspAuditinfo();
            smzSspAuditinfo.setMainId(smzSspProblemmodify.getId());
            smzSspAuditinfo.setProcessState("1");
            smzSspAuditinfo.setCreateUser(smzSspProblemmodify.getCreateUser());
            smzSspAuditinfo.setCreateTime(new Date());
            res = smzSspAuditinfoMapper.insertSmzSspAuditinfo(smzSspAuditinfo);
            smzSspAuditinfo.setFileUrls(smzSspProblemmodify.getSmarkUrl());
            insertSmzSspFileinfo(smzSspAuditinfo);
        }
        return res;
    }

    /**
     * 查询用户类型
     * @param projectId
     * @param createUser
     * @return
     */
    private String getRoleType(Long projectId, String createUser){
        SysDept sysDept = deptMapper.selectDeptByUserName(createUser);
        String roleType = "";
        if(DeptTypeEnum.JTGS.getCode().equals(sysDept.getTypeFlag())){
            SurProjectUserinfo surProjectUserinfo = new SurProjectUserinfo();
            surProjectUserinfo.setProjectId(projectId);
            surProjectUserinfo.setCreateBy(createUser);
            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(projectId);
            surProjectUserinfo.setCreateBy(createUser);
            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();
        }
        return roleType;
    }

    /**
     * 修改随手拍问题整改质量留痕信息
     * 
     * @param smzSspProblemmodify 随手拍问题整改质量留痕信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSmzSspProblemmodifyDraft(SmzSspProblemmodify smzSspProblemmodify)
    {
        //删除草稿
        smzSspProblemmodifyDraftMapper.deleteSmzSspProblemmodifyDraftById(smzSspProblemmodify.getId());
        //设置主图
        smzSspProblemmodify.setMarksPicture(smzSspProblemmodify.getSmarkUrl().split(",")[0]);
        //兼容项目名称为空的异常...
        if(StringUtils.isEmpty(smzSspProblemmodify.getProblemArea())){
            SurProject surProject = surProjectMapper.selectSurProjectById(smzSspProblemmodify.getProjectId());
            smzSspProblemmodify.setProblemArea(surProject.getProjectName());
        }
        if(smzSspProblemmodify.getRoleType()==null || smzSspProblemmodify.getRoleType()<1){
            smzSspProblemmodify.setRoleType(Convert.toInt(getRoleType(smzSspProblemmodify.getProjectId(),smzSspProblemmodify.getCreateUser())));
        }
        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 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);
                }
            }
        }
    }

    /**
     * 批量删除随手拍问题整改质量留痕信息
     * 
     * @param ids 需要删除的随手拍问题整改质量留痕信息主键
     * @return 结果
     */
    @Override
    public int deleteSmzSspProblemmodifyDraftByIds(Long[] ids)
    {
        return smzSspProblemmodifyDraftMapper.deleteSmzSspProblemmodifyDraftByIds(ids);
    }

    /**
     * 删除随手拍问题整改质量留痕信息信息
     * 
     * @param id 随手拍问题整改质量留痕信息主键
     * @return 结果
     */
    @Override
    public int deleteSmzSspProblemmodifyDraftById(Long id)
    {
        return smzSspProblemmodifyDraftMapper.deleteSmzSspProblemmodifyDraftById(id);
    }
}
