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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.changhu.common.enums.ResultCode;
import com.changhu.common.exception.MessageException;
import com.changhu.common.utils.UserUtil;
import com.changhu.module.superManagement.mapper.SuperUnitMapper;
import com.changhu.module.superManagement.pojo.params.ManagementSuperUserSaveOrUpdateParams;
import com.changhu.module.superManagement.pojo.params.UnitCheckPassParams;
import com.changhu.module.superManagement.pojo.queryParams.PoliceUnitPagerQueryParams;
import com.changhu.module.superManagement.pojo.queryParams.SecurityUnitPagerQueryParams;
import com.changhu.module.superManagement.pojo.vo.PoliceUnitPagerVo;
import com.changhu.module.superManagement.pojo.vo.SecurityUnitPagerVo;
import com.changhu.module.superManagement.service.SuperUnitService;
import com.changhu.pojo.entity.ManagementSuperUser;
import com.changhu.pojo.params.UnitDisableOrEnableParams;
import com.changhu.support.mybatisplus.pojo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 20252
 * @createTime 2024/11/21 下午3:11
 * @desc SuperUnitServiceImpl...
 */
@Service
public class SuperUnitServiceImpl implements SuperUnitService {

    @Autowired
    private SuperUnitMapper superUnitMapper;

    @Override
    public void saveOrUpdateUser(ManagementSuperUserSaveOrUpdateParams params) {
        //查看手机号是否存在
        boolean exists = Db.lambdaQuery(ManagementSuperUser.class)
                .eq(ManagementSuperUser::getTelephone, params.getTelephone())
                .exists();
        if (exists) {
            throw new MessageException("手机号已注册");
        }

        //构建用户
        ManagementSuperUser managementSuperUser = ManagementSuperUser.builder()
                .snowFlakeId(params.getSnowFlakeId())
                .name(params.getName())
                .telephone(params.getTelephone())
                .build();

        //如果是新增需要初始化密码
        if (managementSuperUser.getSnowFlakeId() == null) {
            //生成加密密码: salt$$uuid
            String passWordEncrypt = UserUtil.passWordEncrypt(UserUtil.DEFAULT_PASSWORD);
            List<String> saltAndPassWord = StrUtil.split(passWordEncrypt, "$$");
            if (CollUtil.size(saltAndPassWord) != 2) {
                throw new MessageException(ResultCode.ERROR);
            }
            managementSuperUser.setSalt(saltAndPassWord.get(0));
            managementSuperUser.setPassword(saltAndPassWord.get(1));
        }

        //保存到数据库
        Db.saveOrUpdate(managementSuperUser);
    }

    @Override
    public Page<PoliceUnitPagerVo> policeUnitPager(PageParams<PoliceUnitPagerQueryParams, PoliceUnitPagerVo> queryParams) {
        return superUnitMapper.policeUnitPager(queryParams.getPage(), queryParams.getParams());
    }

    @Override
    public Page<SecurityUnitPagerVo> securityUnitPager(PageParams<SecurityUnitPagerQueryParams, SecurityUnitPagerVo> queryParams) {
        return superUnitMapper.securityUnitPager(queryParams.getPage(), queryParams.getParams());
    }

    @Override
    public void checkPass(UnitCheckPassParams params) {
        params.getUnitOptType()
                .getHandler()
                .pass(params.getCheckDataId());
    }

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