package com.yanzhu.project.service.impl;

import java.util.List;

import com.yanzhu.base.domain.BaseAssetsType;
import com.yanzhu.base.mapper.BaseAssetsTypeMapper;
import com.yanzhu.common.core.domain.entity.SysDictData;
import com.yanzhu.common.core.domain.entity.SysUser;
import com.yanzhu.common.enums.ShiFouEnum;
import com.yanzhu.common.utils.DateUtils;
import com.yanzhu.common.utils.DictUtils;
import com.yanzhu.common.utils.SecurityUtils;
import com.yanzhu.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.yanzhu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yanzhu.project.domain.ProProjectApplyDetail;
import com.yanzhu.project.mapper.ProProjectApplyMapper;
import com.yanzhu.project.domain.ProProjectApply;
import com.yanzhu.project.service.IProProjectApplyService;

/**
 * 项目申请Service业务层处理
 * 
 * @author yanZhu
 * @date 2024-02-23
 */
@Service
public class ProProjectApplyServiceImpl implements IProProjectApplyService 
{
    @Autowired
    private ProProjectApplyMapper proProjectApplyMapper;

    @Autowired
    private BaseAssetsTypeMapper baseAssetsTypeMapper;

    /**
     * 查询项目申请
     * 
     * @param id 项目申请主键
     * @return 项目申请
     */
    @Override
    public ProProjectApply selectProProjectApplyById(Long id)
    {
        return proProjectApplyMapper.selectProProjectApplyById(id);
    }

    /**
     * 查询项目申请列表
     * 
     * @param proProjectApply 项目申请
     * @return 项目申请
     */
    @Override
    public List<ProProjectApply> selectProProjectApplyList(ProProjectApply proProjectApply)
    {
        return proProjectApplyMapper.selectProProjectApplyList(proProjectApply);
    }

    /**
     * 新增项目申请
     * 
     * @param proProjectApply 项目申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProProjectApply(ProProjectApply proProjectApply)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        proProjectApply.setDeptId(sysUser.getDeptId());
        proProjectApply.setParProjName(sysUser.getParDeptName());
        proProjectApply.setApplyUser(sysUser.getUserId());
        proProjectApply.setIsDel(ShiFouEnum.FOU.getCode());
        proProjectApply.setCreateBy(sysUser.getNickName());
        proProjectApply.setCreateTime(DateUtils.getNowDate());
        int rows = proProjectApplyMapper.insertProProjectApply(proProjectApply);
        insertProProjectApplyDetail(proProjectApply);
        return rows;
    }

    /**
     * 修改项目申请
     * 
     * @param proProjectApply 项目申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProProjectApply(ProProjectApply proProjectApply)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        proProjectApply.setCreateTime(DateUtils.getNowDate());
        proProjectApply.setUpdateBy(sysUser.getNickName());
        proProjectApply.setUpdateTime(DateUtils.getNowDate());
        proProjectApplyMapper.deleteProProjectApplyDetailByApplyId(proProjectApply.getId());
        insertProProjectApplyDetail(proProjectApply);
        return proProjectApplyMapper.updateProProjectApply(proProjectApply);
    }

    /**
     * 批量删除项目申请
     * 
     * @param ids 需要删除的项目申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProProjectApplyByIds(Long[] ids)
    {
        proProjectApplyMapper.deleteProProjectApplyDetailByApplyIds(ids);
        return proProjectApplyMapper.deleteProProjectApplyByIds(ids);
    }

    /**
     * 删除项目申请信息
     * 
     * @param id 项目申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProProjectApplyById(Long id)
    {
        proProjectApplyMapper.deleteProProjectApplyDetailByApplyId(id);
        return proProjectApplyMapper.deleteProProjectApplyById(id);
    }

    /**
     * 新增项目申请明细信息
     * 
     * @param proProjectApply 项目申请对象
     */
    public void insertProProjectApplyDetail(ProProjectApply proProjectApply)
    {
        List<ProProjectApplyDetail> proProjectApplyDetailList = proProjectApply.getProProjectApplyDetailList();
        Long id = proProjectApply.getId();
        if (StringUtils.isNotNull(proProjectApplyDetailList))
        {
            String superTypeName = DictUtils.getDictLabel("sys_process_category", proProjectApply.getApplyType());
            List<ProProjectApplyDetail> list = new ArrayList<ProProjectApplyDetail>();
            for (ProProjectApplyDetail proProjectApplyDetail : proProjectApplyDetailList)
            {
                proProjectApplyDetail.setApplyId(id);
                proProjectApplyDetail.setSuperTypeKey(proProjectApply.getApplyType());
                proProjectApplyDetail.setSuperTypeName(superTypeName);
                //资产
                BaseAssetsType baseAssetsType = baseAssetsTypeMapper.selectBaseAssetsTypeById(proProjectApplyDetail.getAssetsId());
                proProjectApplyDetail.setAssetsName(baseAssetsType.getName());
                //资产类型
                BaseAssetsType parentAssetsType = baseAssetsTypeMapper.selectBaseAssetsTypeById(baseAssetsType.getParentId());
                proProjectApplyDetail.setTypeId(parentAssetsType.getId());
                proProjectApplyDetail.setTypeName(parentAssetsType.getName());
                proProjectApplyDetail.setIsDel(ShiFouEnum.FOU.getCode());
                list.add(proProjectApplyDetail);
            }
            if (list.size() > 0)
            {
                proProjectApplyMapper.batchProProjectApplyDetail(list);
            }
        }
    }
}
