package com.changhu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.changhu.common.db.enums.CheckStatus;
import com.changhu.common.db.enums.IsEnable;
import com.changhu.common.enums.ResultCode;
import com.changhu.common.exception.MessageException;
import com.changhu.common.pojo.model.JsonResult;
import com.changhu.common.pojo.vo.SelectNodeVo;
import com.changhu.module.management.mapper.PoliceUnitMapper;
import com.changhu.module.management.mapper.SecurityUnitMapper;
import com.changhu.module.management.pojo.entity.PoliceUnit;
import com.changhu.module.management.pojo.entity.SecurityUnit;
import com.changhu.pojo.params.PoliceUnitRegisterParams;
import com.changhu.pojo.params.SecurityUnitRegisterParams;
import com.changhu.pojo.queryParams.QueryUnitListByIdentityQueryParams;
import com.changhu.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author 20252
 * @createTime 2024/8/30 下午3:18
 * @desc CommonServiceImpl...
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private SecurityUnitMapper securityUnitMapper;

    @Autowired
    private PoliceUnitMapper policeUnitMapper;


    @Override
    public JsonResult<Void> securityUnitRegister(SecurityUnitRegisterParams params) {
        //先查看该社会编码是否存在
        boolean exists = securityUnitMapper.exists(Wrappers.<SecurityUnit>lambdaQuery().eq(SecurityUnit::getSocialCode, params.getSocialCode()));
        if (exists) {
            throw new MessageException("该单位已存在或已提交申请!");
        }
        SecurityUnit securityUnit = BeanUtil.copyProperties(params, SecurityUnit.class);
        Optional.ofNullable(params.getAdministrativeDivisionCodes())
                .ifPresent(codes -> {
                    if (!codes.isEmpty()) {
                        securityUnit.setProvince(codes.get(0));
                    }
                    if (codes.size() >= 2) {
                        securityUnit.setCity(codes.get(1));
                    }
                    if (codes.size() >= 3) {
                        securityUnit.setDistricts(codes.get(2));
                    }
                    if (codes.size() >= 4) {
                        securityUnit.setStreet(codes.get(3));
                    }
                });
        int i = securityUnitMapper.insert(securityUnit);
        if (i <= 0) {
            throw new MessageException(ResultCode.ERROR);
        }

        return JsonResult.successMsg("申请已提交,请等待审核");
    }

    @Override
    public JsonResult<Void> policeUnitRegister(PoliceUnitRegisterParams params) {
        //先查看该公安单位是否存在
        boolean exists = policeUnitMapper.exists(Wrappers.<PoliceUnit>lambdaQuery().eq(PoliceUnit::getCode, params.getCode()));
        if (exists) {
            throw new MessageException("该单位已存在或已提交申请!");
        }
        PoliceUnit policeUnit = BeanUtil.copyProperties(params, PoliceUnit.class);
        Optional.ofNullable(params.getAdministrativeDivisionCodes())
                .ifPresent(codes -> {
                    if (!codes.isEmpty()) {
                        policeUnit.setProvince(codes.get(0));
                    }
                    if (codes.size() >= 2) {
                        policeUnit.setCity(codes.get(1));
                    }
                    if (codes.size() >= 3) {
                        policeUnit.setDistricts(codes.get(2));
                    }
                    if (codes.size() >= 4) {
                        policeUnit.setStreet(codes.get(3));
                    }
                });
        int i = policeUnitMapper.insert(policeUnit);
        if (i <= 0) {
            throw new MessageException(ResultCode.ERROR);
        }

        return JsonResult.successMsg("申请已提交,请等待审核");
    }

    @Override
    public List<SelectNodeVo<Long>> queryUnitListByIdentity(QueryUnitListByIdentityQueryParams queryParams) {
        List<String> administrativeDivisionCodes = queryParams.getAdministrativeDivisionCodes();
        return switch (queryParams.getIdentity()) {
            case POLICE -> policeUnitMapper.selectList(Wrappers.<PoliceUnit>lambdaQuery()
                            .eq(PoliceUnit::getIsEnable, IsEnable.TRUE)
                            .eq(PoliceUnit::getCheckStatus, CheckStatus.checked)
                            .eq(!administrativeDivisionCodes.isEmpty(), PoliceUnit::getProvince, administrativeDivisionCodes.get(0))
                            .eq(administrativeDivisionCodes.size() >= 2, PoliceUnit::getCity, administrativeDivisionCodes.get(1))
                            .eq(administrativeDivisionCodes.size() >= 3, PoliceUnit::getDistricts, administrativeDivisionCodes.get(2))
                            .eq(administrativeDivisionCodes.size() >= 4, PoliceUnit::getStreet, administrativeDivisionCodes.get(3)))
                    .stream()
                    .map(item -> SelectNodeVo.<Long>builder()
                            .value(item.getSnowFlakeId())
                            .label(item.getName())
                            .build())
                    .toList();
            case PROJECT_MANAGER -> securityUnitMapper.selectList(Wrappers.<SecurityUnit>lambdaQuery()
                            .eq(SecurityUnit::getIsEnable, IsEnable.TRUE)
                            .eq(SecurityUnit::getCheckStatus, CheckStatus.checked)
                            .eq(!administrativeDivisionCodes.isEmpty(), SecurityUnit::getProvince, administrativeDivisionCodes.get(0))
                            .eq(administrativeDivisionCodes.size() >= 2, SecurityUnit::getCity, administrativeDivisionCodes.get(1))
                            .eq(administrativeDivisionCodes.size() >= 3, SecurityUnit::getDistricts, administrativeDivisionCodes.get(2))
                            .eq(administrativeDivisionCodes.size() >= 4, SecurityUnit::getStreet, administrativeDivisionCodes.get(3)))
                    .stream()
                    .map(item -> SelectNodeVo.<Long>builder()
                            .value(item.getSnowFlakeId())
                            .label(item.getName())
                            .build())
                    .toList();
        };
    }
}
