package com.changhu.module.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.func.LambdaUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changhu.common.db.enums.UserType;
import com.changhu.common.exception.MessageException;
import com.changhu.common.pojo.vo.SelectNodeVo;
import com.changhu.common.utils.UserUtil;
import com.changhu.module.management.mapper.EnterprisesUnitMapper;
import com.changhu.module.management.pojo.entity.EnterprisesUnit;
import com.changhu.module.management.pojo.params.EnterprisesUnitSaveOrUpdateParams;
import com.changhu.module.management.pojo.queryParams.EnterprisesUnitPagerQueryParams;
import com.changhu.module.management.pojo.vo.EnterprisesUnitPagerVo;
import com.changhu.module.management.service.EnterprisesUnitService;
import com.changhu.support.mybatisplus.pojo.params.PageParams;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * enterprises_unit (企事业单位)  服务实现类
 * author: luozhun
 * desc 由groovy脚本自动生成
 */
@Service
public class EnterprisesUnitServiceImpl extends ServiceImpl<EnterprisesUnitMapper, EnterprisesUnit> implements EnterprisesUnitService {

    @Override
    public Page<EnterprisesUnitPagerVo> pager(PageParams<EnterprisesUnitPagerQueryParams, EnterprisesUnitPagerVo> queryParams) {
        return switch (UserUtil.getUserType()) {
            case MANAGEMENT_POLICE -> baseMapper.pager(queryParams.getPage(), UserUtil.getUnitId(), queryParams.getParams());
            case MANAGEMENT_SUPER -> baseMapper.pager(queryParams.getPage(), null, queryParams.getParams());
            default -> throw new MessageException();
        };
    }

    @Override
    public void saveOrUpdate(EnterprisesUnitSaveOrUpdateParams params) {
        EnterprisesUnit enterprisesUnit = BeanUtil.copyProperties(params, EnterprisesUnit.class);
        //需要将行政区划编码转换为省市区
        Optional.ofNullable(params.getAdministrativeDivisionCodes())
                .ifPresent(codes -> {
                    if (!codes.isEmpty()) {
                        enterprisesUnit.setProvince(codes.get(0));
                    }
                    if (codes.size() >= 2) {
                        enterprisesUnit.setCity(codes.get(1));
                    }
                    if (codes.size() >= 3) {
                        enterprisesUnit.setDistricts(codes.get(2));
                    }
                    if (codes.size() >= 4) {
                        enterprisesUnit.setStreet(codes.get(3));
                    }
                });
        //如果身份是公安的 是不会携带单位id的 从用户信息里取出来
        if (UserType.MANAGEMENT_POLICE.equals(UserUtil.getUserType())) {
            enterprisesUnit.setPoliceUnitId(UserUtil.getUnitId());
        }
        boolean b = this.saveOrUpdate(enterprisesUnit);
        if (!b) {
            throw new MessageException();
        }
    }

    @Override
    public List<SelectNodeVo<Long>> queryListByAdministrativeDivisionCodes(List<String> administrativeDivisionCodes) {
        String addr = LambdaUtil.getFieldName(EnterprisesUnit::getAddress);
        String rem = LambdaUtil.getFieldName(EnterprisesUnit::getRemark);
        return this.lambdaQuery()
                .eq(!administrativeDivisionCodes.isEmpty(), EnterprisesUnit::getProvince, administrativeDivisionCodes.get(0))
                .eq(administrativeDivisionCodes.size() >= 2, EnterprisesUnit::getCity, administrativeDivisionCodes.get(1))
                .eq(administrativeDivisionCodes.size() >= 3, EnterprisesUnit::getDistricts, administrativeDivisionCodes.get(2))
                .eq(administrativeDivisionCodes.size() >= 4, EnterprisesUnit::getStreet, administrativeDivisionCodes.get(3))
                .list()
                .stream()
                .map(item -> SelectNodeVo.<Long>builder()
                        .value(item.getSnowFlakeId())
                        .label(item.getName())
                        .extData(Dict.of(
                                addr, item.getAddress(),
                                rem, item.getRemark()
                        ))
                        .build())
                .toList();
    }
}
