package com.changhu.module.management.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.management.mapper.PoliceUnitMapper;
import com.changhu.module.management.mapper.SecurityUnitMapper;
import com.changhu.module.management.pojo.entity.ManagementSuperUser;
import com.changhu.module.management.pojo.params.ManagementSuperUserSaveOrUpdateParams;
import com.changhu.module.management.pojo.queryParams.PoliceUnitPagerQueryParams;
import com.changhu.module.management.pojo.queryParams.SecurityUnitPagerQueryParams;
import com.changhu.module.management.pojo.vo.PoliceUnitPagerVo;
import com.changhu.module.management.pojo.vo.SecurityUnitPagerVo;
import com.changhu.module.management.service.SuperService;
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/9/11 上午9:52
 * @desc SuperServiceImpl...
 */
@Service
public class SuperServiceImpl implements SuperService {

    @Autowired
    private PoliceUnitMapper policeUnitMapper;

    @Autowired
    private SecurityUnitMapper securityUnitMapper;

    @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 policeUnitMapper.pager(queryParams.getPage(), queryParams.getParams());
    }

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