package com.changhu.service.impl;

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.toolkit.Db;
import com.changhu.common.db.enums.IsEnable;
import com.changhu.common.db.enums.MiniProgramUserIdentity;
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.mapper.ManagementMapper;
import com.changhu.pojo.entity.MiniProgramUser;
import com.changhu.pojo.entity.SecurityUnit;
import com.changhu.pojo.params.CheckStatusParams;
import com.changhu.pojo.params.UnitDisableOrEnableParams;
import com.changhu.pojo.vo.UnitCheckStatusVo;
import com.changhu.pojo.vo.UnitMiniProgramUserPagerQueryParams;
import com.changhu.pojo.vo.UnitMiniProgramUserPagerVo;
import com.changhu.service.ManagementService;
import com.changhu.support.mybatisplus.pojo.params.PageParams;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 20252
 * @createTime 2024/9/2 上午9:49
 * @desc IndexServiceImpl...
 */
@Service
public class ManagementServiceImpl implements ManagementService {

    @Resource
    private ManagementMapper managementMapper;

    @Override
    public UnitCheckStatusVo getCheckStatus(CheckStatusParams params) {
        return params.getUnitOptType()
                .getHandler()
                .getCheckStatus(params.getOnlyCode());
    }

    @Override
    public Page<UnitMiniProgramUserPagerVo> miniProgramUserPager(PageParams<UnitMiniProgramUserPagerQueryParams, UnitMiniProgramUserPagerVo> queryParams) {
        UserType userType = UserUtil.getUserType();
        MiniProgramUserIdentity identity = switch (userType) {
            case MANAGEMENT_POLICE -> MiniProgramUserIdentity.POLICE;
            case MANAGEMENT_SECURITY -> MiniProgramUserIdentity.PROJECT_MANAGER;
            default -> throw new MessageException("用户类型不匹配");
        };
        return managementMapper.miniProgramUserPager(queryParams.getPage(), queryParams.getParams(), identity);
    }

    @Override
    public void passMiniProgramUser(UnitDisableOrEnableParams params) {
        params.getUnitOptType()
                .getHandler()
                .passMiniProgramUser(params.getDataId());
    }

    @Override
    public void disableOrEnableMiniProgramUser(UnitDisableOrEnableParams params) {
        params.getUnitOptType()
                .getHandler()
                .disableOrEnableMiniProgramUser(params.getDataId());
    }

    @Override
    public List<SelectNodeVo<Long>> listSecurityUnit() {
        String addressName = LambdaUtil.getFieldName(SecurityUnit::getAddress);
        String legalPersonInfoName = LambdaUtil.getFieldName(SecurityUnit::getLegalPersonInfo);
        return Db.lambdaQuery(SecurityUnit.class)
                .eq(SecurityUnit::getIsEnable, IsEnable.TRUE)
                .list()
                .stream()
                .map(unit -> SelectNodeVo.<Long>builder()
                        .value(unit.getSnowFlakeId())
                        .label(unit.getName())
                        .extData(Dict.of(
                                addressName, unit.getAddress(),
                                legalPersonInfoName, unit.getLegalPersonInfo()))
                        .build())
                .toList();
    }

    @Override
    public List<SelectNodeVo<Long>> listMinUserByUnitId(Long securityUnitId, MiniProgramUserIdentity identity) {
        String tel = LambdaUtil.getFieldName(MiniProgramUser::getTelephone);
        String sex = LambdaUtil.getFieldName(MiniProgramUser::getSex);
        return Db.lambdaQuery(MiniProgramUser.class)
                .eq(MiniProgramUser::getIsEnable, IsEnable.TRUE)
                .eq(MiniProgramUser::getIdentity, identity)
                .eq(MiniProgramUser::getUnitId, securityUnitId)
                .list()
                .stream()
                .map(item -> SelectNodeVo.<Long>builder()
                        .value(item.getSnowFlakeId())
                        .label(item.getName())
                        .extData(Dict.of(
                                tel, item.getTelephone(),
                                sex, item.getSex()
                        ))
                        .build())
                .toList();
    }
}
