package com.ruoyi.project.business.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.enums.SerialNumberPrefix;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SpecialCharacterEscaper;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.business.domain.TjElevator;
import com.ruoyi.project.business.domain.TjElevatorParaDetail;
import com.ruoyi.project.business.domain.vo.selectParam.CivilEngineeringParam;
import com.ruoyi.project.business.enums.PlotStatusEnum;
import com.ruoyi.project.business.mapper.TjElevatorMapper;
import com.ruoyi.project.business.mapper.TjElevatorParaDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.mapper.TjCivilEngineeringMapper;
import com.ruoyi.project.business.domain.TjCivilEngineering;
import com.ruoyi.project.business.service.ITjCivilEngineeringService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【土建出图】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-20
 */
@Service
public class TjCivilEngineeringServiceImpl implements ITjCivilEngineeringService
{
    @Autowired
    private TjCivilEngineeringMapper tjCivilEngineeringMapper;

    @Autowired
    private TjElevatorMapper tjElevatorMapper;

    @Autowired
    private TjElevatorParaDetailMapper tjElevatorParaDetailMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询【土建出图】
     * 
     * @param id 【土建出图】主键
     * @return 【土建出图】
     */
    @Override
    public TjCivilEngineering selectTjCivilEngineeringById(Long id)
    {
        TjCivilEngineering tjCivilEngineering = tjCivilEngineeringMapper.selectTjCivilEngineeringById(id);
        // 查询土建出图关联电梯
        List<TjElevator> tjElevators = tjElevatorMapper.selectElevatorListByCivilEngineeringId(id);
        List<Long> elevatorIds = tjElevators.stream().map(TjElevator::getId).collect(Collectors.toList());
        // 查询土建出图关联电梯的所有参数
        List<TjElevatorParaDetail> detailList = tjElevatorParaDetailMapper.selectByElevatorIds(elevatorIds);
        // 根据电梯id分组参数
        tjElevators.forEach(elevator -> elevator.setParamList(detailList.stream().filter(detail -> detail.getElevatorId().equals(elevator.getId())).collect(Collectors.toList())));
        tjCivilEngineering.setElevatorList(tjElevators);
        return tjCivilEngineering;
    }

    /**
     * 查询【土建出图】列表
     * 
     * @param tjCivilEngineering 【土建出图】
     * @return 【土建出图】
     */
    @Override
    public List<TjCivilEngineering> selectTjCivilEngineeringList(CivilEngineeringParam tjCivilEngineering)
    {
        tjCivilEngineering.setPermissionChar(SecurityUtils.getPermissionCharSea());
        SpecialCharacterEscaper.escapeSpecialCharacters(tjCivilEngineering, CivilEngineeringParam::getProjectNo, CivilEngineeringParam::getProjectName,
                CivilEngineeringParam::getCivilEngineeringNo, CivilEngineeringParam::getQuotationNo, CivilEngineeringParam::getElevatorModel);
        return tjCivilEngineeringMapper.selectTjCivilEngineeringList(tjCivilEngineering);
    }

    /**
     * 新增【土建出图】
     * 
     * @param tjCivilEngineering 【土建出图】
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTjCivilEngineering(TjCivilEngineering tjCivilEngineering)
    {
        // 土建出图编号
        tjCivilEngineering.setCivilEngineeringNo(redisCache.generateSerialNumber(SerialNumberPrefix.CE.getPrefix()));
        // 默认出图状态为出图中
        tjCivilEngineering.setStatus(PlotStatusEnum.PROGRESSING.getValue());
        tjCivilEngineering.setPermissionChar(SecurityUtils.getPermissionCharIns());
        String userId = SecurityUtils.getStrUserId();
        tjCivilEngineering.setCreateBy(userId);
        tjCivilEngineering.setUpdateBy(userId);
        Date nowDate = DateUtils.getNowDate();
        tjCivilEngineering.setCreateTime(nowDate);
        tjCivilEngineering.setUpdateTime(nowDate);
        int result = tjCivilEngineeringMapper.insertTjCivilEngineering(tjCivilEngineering);
        List<TjElevator> elevatorList = tjCivilEngineering.getElevatorList();
        elevatorList.forEach(elevator -> {
            elevator.setCivilEngineeringId(tjCivilEngineering.getId());
            elevator.setQuotationId(tjCivilEngineering.getQuotationId());
            elevator.setCreateBy(userId);
            elevator.setCreateTime(nowDate);
            // 新增电梯
            tjElevatorMapper.insertTjElevator(elevator);
            List<TjElevatorParaDetail> paramList = elevator.getParamList();
            if (paramList != null && !paramList.isEmpty()) {
                paramList.forEach(param -> {
                    param.setElevatorId(elevator.getId());
                    param.setCreateBy(userId);
                    param.setUpdateBy(userId);
                    param.setCreateTime(nowDate);
                    param.setUpdateTime(nowDate);
                });
                // 批量新增电梯参数
                tjElevatorParaDetailMapper.batchInsertTjElevatorParaDetail(paramList);
            }
        });
        return result;
    }

    /**
     * 修改【土建出图】
     * 
     * @param tjCivilEngineering 【土建出图】
     * @return 结果
     */
    @Override
    public int updateTjCivilEngineering(TjCivilEngineering tjCivilEngineering)
    {
        tjCivilEngineering.setUpdateTime(DateUtils.getNowDate());
        return tjCivilEngineeringMapper.updateTjCivilEngineering(tjCivilEngineering);
    }

    /**
     * 批量删除【土建出图】
     * 
     * @param ids 需要删除的【土建出图】主键
     * @return 结果
     */
    @Override
    public int deleteTjCivilEngineeringByIds(Long[] ids)
    {
        return tjCivilEngineeringMapper.deleteTjCivilEngineeringByIds(ids);
    }

    /**
     * 删除【土建出图】信息
     * 
     * @param id 【土建出图】主键
     * @return 结果
     */
    @Override
    public int deleteTjCivilEngineeringById(Long id)
    {
        return tjCivilEngineeringMapper.deleteTjCivilEngineeringById(id);
    }
}
