/*
 * Author: Zev
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.daily.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.api.UserClent;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.Constant;
import com.tofly.pojo.User;
import com.tofly.pojo.plan.dto.RegionrespDto;
import com.tofly.pojo.plan.vo.RegionrespVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.mapper.plan.RegionrespMapper;
import com.tofly.gpsboot.service.config.InspectgroupService;
import com.tofly.gpsboot.service.daily.RegionrespService;
import com.tofly.gpsboot.service.paln.task.PlanService;
import com.tofly.pojo.plan.Inspectgroup;
import com.tofly.pojo.plan.Plan;
import com.tofly.pojo.plan.Regionresp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 片区责任组关联信息表：TF_YWPN_REGIONRESP_W
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@Service("regionrespService")
public class RegionrespServiceImpl extends ServiceImpl<RegionrespMapper, Regionresp> implements RegionrespService {
    @Autowired
    private InspectgroupService inspectgroupService;
    @Autowired
    private PlanService planService;
    @Autowired
    private UserClent userClent;
    @Override
    public List<RegionrespVo> viewConversion(List<Regionresp> infoList) {
        List<RegionrespVo> voList = new ArrayList<>();
        infoList.forEach(info -> {
            RegionrespVo vo = JSON.parseObject(JSON.toJSONString(info), RegionrespVo.class);
            // 新增巡检组名称
            long respGroupId = info.getRespGroup().longValue();
            Inspectgroup byId = inspectgroupService.getById(respGroupId);
            if(null!=byId){
                vo.setRespGroupName(byId.getName());
            }

            User result = userClent.getById(info.getCreateUser()).getResult();
            if(null!=result){
                vo.setCreateUserName(result.getRealName());
            }
            voList.add(vo);
        });
        return voList;
    }

    @Override
    public Page<RegionrespVo> getRegionrespPage(Page page, RegionrespDto dto) {
        // 获取当前部门下的所有用户的巡检租
        QueryWrapper<Regionresp> queryWrapper = new QueryWrapper<>();
        Long departmentId = SecurityUtils.getUser().getDepartmentId();
        Set<Long> inspectGroupIdSet = inspectgroupService.list(new QueryWrapper<Inspectgroup>().lambda().in(Inspectgroup::getDeptId, departmentId)).stream().map(Inspectgroup::getId).collect(Collectors.toSet());
        BigDecimal respGroup = dto.getRespGroup();
        if(null!=respGroup&&BigDecimal.ZERO.compareTo(respGroup)<0){
            queryWrapper.in("RESP_GROUP",respGroup);
        }else {
            if(inspectGroupIdSet.size()>0){
                queryWrapper.in("RESP_GROUP", inspectGroupIdSet);
            }

        }

        String regionName = dto.getRegionName();
        if(!StringUtils.isEmpty(regionName)){
            queryWrapper.like("REGION_NAME",regionName);
        }
        Page infoPageList = this.page(page, queryWrapper);
        infoPageList.setRecords(this.viewConversion(infoPageList.getRecords()));
        return infoPageList;
    }

    @Override
    public void deleteRegionrespInfo(String idLists) throws BusinessException {
        // 判断当前是否有关联数据 计划
        List<Long> longList = Arrays.stream(idLists.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        Set<String> regionCodeList = this.list(new QueryWrapper<Regionresp>().lambda().in(Regionresp::getId, longList)).stream().map(Regionresp::getRegionId).collect(Collectors.toSet());
        List<Plan> list = planService.list(new QueryWrapper<Plan>().lambda().in(Plan::getRegionId, regionCodeList));
        if(null!=list && list.size()>0){
            throw new BusinessException(Constant.REGION_TYPE_NO_DELETE_CODE,Constant.REGION_TYPE_NO_DELETE);
        }else {
            this.removeByIds(longList);
        }
    }
}
