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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
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.EnterprisesUnitMapper;
import com.changhu.module.management.mapper.ManagementPoliceUnitUserMapper;
import com.changhu.module.management.pojo.entity.ManagementPoliceUnitUser;
import com.changhu.module.management.pojo.params.ManagementPoliceUserSaveOrUpdateParams;
import com.changhu.module.management.pojo.queryParams.ManagementPoliceUnitUserPagerQueryParams;
import com.changhu.module.management.pojo.vo.ManagementPoliceUnitUserPagerVo;
import com.changhu.module.management.service.PoliceService;
import com.changhu.support.mybatisplus.pojo.entity.BaseEntity;
import com.changhu.support.mybatisplus.pojo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 20252
 * @createTime 2024/9/11 上午9:59
 * @desc MPoliceServiceImpl...
 */
@Service
public class PoliceServiceImpl implements PoliceService {

    @Autowired
    private ManagementPoliceUnitUserMapper managementPoliceUnitUserMapper;

    @Autowired
    private EnterprisesUnitMapper enterprisesUnitMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void userSaveOrUpdate(ManagementPoliceUserSaveOrUpdateParams params) {
        //查看手机号是否存在
        boolean exists = Db.lambdaQuery(ManagementPoliceUnitUser.class)
                .ne(params.getSnowFlakeId() != null, BaseEntity::getSnowFlakeId, params.getSnowFlakeId())
                .eq(ManagementPoliceUnitUser::getTelephone, params.getTelephone())
                .exists();
        if (exists) {
            throw new MessageException("该手机号已存在");
        }
        ManagementPoliceUnitUser managementPoliceUnitUser = BeanUtil.copyProperties(params, ManagementPoliceUnitUser.class);
        //新增 补全信息
        if (managementPoliceUnitUser.getSnowFlakeId() == null) {
            String account = RandomUtil.randomString(6);
            boolean accExits = Db.lambdaQuery(ManagementPoliceUnitUser.class).eq(ManagementPoliceUnitUser::getAccount, account).exists();
            if (accExits) {
                throw new MessageException("生成的账号已存在 请重新提交生成");
            }
            //生成账号
            managementPoliceUnitUser.setAccount(account);
            //补全单位
            managementPoliceUnitUser.setPoliceUnitId(UserUtil.getUnitId());
            //生成密码
            String passWordEncrypt = UserUtil.passWordEncrypt(UserUtil.DEFAULT_PASSWORD);
            List<String> saltAndPassWord = StrUtil.split(passWordEncrypt, "$$");
            managementPoliceUnitUser.setSalt(saltAndPassWord.get(0));
            managementPoliceUnitUser.setPassword(saltAndPassWord.get(1));
        }
        Db.saveOrUpdate(managementPoliceUnitUser);
    }

    @Override
    public Page<ManagementPoliceUnitUserPagerVo> userPager(PageParams<ManagementPoliceUnitUserPagerQueryParams, ManagementPoliceUnitUserPagerVo> queryParams) {
        return managementPoliceUnitUserMapper.pager(queryParams.getPage(), queryParams.getParams());
    }

    @Override
    public void userDeleteById(Long managementPoliceUnitUserId) {
        Long unitId = UserUtil.getUnitId();
        Long l = Db.lambdaQuery(ManagementPoliceUnitUser.class)
                .eq(BaseEntity::getSnowFlakeId, managementPoliceUnitUserId)
                .oneOpt()
                .map(ManagementPoliceUnitUser::getPoliceUnitId)
                .orElseThrow(() -> new MessageException(ResultCode.USER_NOT_FOUND));
        if (!unitId.equals(l)) {
            throw new MessageException("单位不匹配,无权操作");
        }
        boolean b = Db.removeById(managementPoliceUnitUserId, ManagementPoliceUnitUser.class);
        if (!b) {
            throw new MessageException();
        }
    }

}
