package com.ruoyi.ehsRescueTeam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ehsRescueTeam.domain.EhsRescueTeam;
import com.ruoyi.ehsRescueTeam.domain.RescueTeamImportTemplate;
import com.ruoyi.ehsRescueTeam.mapper.EhsRescueTeamMapper;
import com.ruoyi.ehsRescueTeam.service.IEhsRescueTeamService;
import com.ruoyi.ehsRescueTeam.vo.TeamResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 应急救援队伍管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@Service
public class EhsRescueTeamServiceImpl extends ServiceImpl<EhsRescueTeamMapper, EhsRescueTeam> implements IEhsRescueTeamService {

    @Autowired
    private EhsRescueTeamMapper ehsRescueTeamMapper;

    /**
     * 查询应急救援队伍管理
     *
     * @param id 应急救援队伍管理主键
     * @return 应急救援队伍管理
     */
    @Override
    public EhsRescueTeam selectEhsRescueTeamById(Long id) {
        return ehsRescueTeamMapper.selectEhsRescueTeamById(id);
    }


    /**
     * 查询应急救援队伍管理列表
     *
     * @param ehsRescueTeam 应急救援队伍管理
     * @return 应急救援队伍管理
     */
    @Override
    public List<EhsRescueTeam> selectEhsRescueTeamList(EhsRescueTeam ehsRescueTeam) {
        // 如果不是局端,那么只能查询自己部门下的数据
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        Long deptParentId = currentUser.getDept().getParentId();
        if (deptParentId != null &&
                deptParentId.longValue() != 0) {
            ehsRescueTeam.setDeptId(currentUser.getDept().getDeptId().longValue());
        }
        List<EhsRescueTeam> ehsRescueTeamList = ehsRescueTeamMapper.selectEhsRescueTeamList(ehsRescueTeam);
        // 给队伍设置人数信息
        ehsRescueTeamList.stream().forEach(item->{
            Long count = ehsRescueTeamMapper.getWorkerNumByTeamId(item.getId());
            if (count != null){
                item.setRescueWorkerNum(count);
            }
        });

        return ehsRescueTeamList;
    }

    /**
     * 应急救援:根据队伍id获取队伍信息
     */
    @Override
    public TeamResponseVo getTeamInfoByTeamId(Long rescueTeamId) {
        return ehsRescueTeamMapper.getTeamInfoByTeamId(rescueTeamId);
    }

    /**
     * 导入模板数据
     * @param list
     * @param updateSupport
     * @return
     */
    @Override
    public String importRescueTeam(List<RescueTeamImportTemplate> list, Boolean updateSupport) throws Exception {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new Exception("导入救援队伍信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RescueTeamImportTemplate rescueTeamTemplate : list) {

            EhsRescueTeam temp = new EhsRescueTeam();
            /**
             * 因为是插入数据,所以id需要处理,如果id是String类型,那么生成个uuid插入到数据库
             * 如果id是整数,并且自动递增,那么无需设置id
             */
            // temp.setId(IdUtils.fastSimpleUUID());
            temp.setTeamName(rescueTeamTemplate.getTeamName());  //队伍名称
            temp.setTeamLevel(rescueTeamTemplate.getTeamLevel()); //队伍级别
            temp.setDutyer(rescueTeamTemplate.getDutyer());// 负责人
            temp.setPhone(rescueTeamTemplate.getPhone());// 联系电话
            temp.setUnit(rescueTeamTemplate.getUnit());// 所属单位
            temp.setStation(rescueTeamTemplate.getStation());// 驻地
            temp.setTeamProperty(rescueTeamTemplate.getTeamProperty());// 队伍属性
            temp.setRemark(rescueTeamTemplate.getRemark()); // 备注
            try {
                //插入数据
                this.save(temp);
                successNum++;
                successMsg.append("<br />" + successNum + "、应急救援队伍信息" + temp.getTeamName() + "导入成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getLocalizedMessage());
                failureNum++;
                successMsg.append("<br /><font color='red'>" + failureNum + "、应急救援队伍信息" + temp.getTeamName() + "导入异常，请联系管理员！</font>");
                continue;
            }
        }
        return successMsg.toString();
    }



    private LambdaQueryWrapper<EhsRescueTeam> buildQueryWrapper(EhsRescueTeam query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<EhsRescueTeam> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getTeamName()), EhsRescueTeam::getTeamName, query.getTeamName());
        lqw.like(StringUtils.isNotBlank(query.getTeamLevel()), EhsRescueTeam::getTeamLevel, query.getTeamLevel());
        lqw.orderByDesc(EhsRescueTeam::getCreateTime);
        lqw.like(StringUtils.isNotBlank(query.getDutyer()), EhsRescueTeam::getDutyer, query.getDutyer());
        lqw.eq(StringUtils.isNotBlank(query.getPhone()), EhsRescueTeam::getPhone, query.getPhone());
        lqw.eq(StringUtils.isNotBlank(query.getUnit()), EhsRescueTeam::getUnit, query.getUnit());
        lqw.like(StringUtils.isNotBlank(query.getStation()), EhsRescueTeam::getStation, query.getStation());
        return lqw;
    }

}
