package com.mosty.base.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.manager.mapper.TbJcglPzfzMapper;
import com.mosty.base.manager.service.TbJcglPzfzService;
import com.mosty.base.model.dto.jcgl.TbJcglPzfzDto;
import com.mosty.base.model.entity.jcgl.TbJcglPzfz;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TbJcglPzfzServiceImpl extends ServiceImpl<TbJcglPzfzMapper, TbJcglPzfz> implements TbJcglPzfzService {

    @Override
    public IPage<Map<String, Object>> getPageList(TbJcglPzfzDto dto) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", (dto.getPageCurrent() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
        map.put("dto", dto);
        List<Map<String, Object>> list = this.baseMapper.getList(map);
        int count = this.baseMapper.getListCount(map);
        IPage<Map<String, Object>> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        page.setRecords(list);
        page.setTotal(count);
        return page;
    }

    @Override
    public List<TbJcglPzfz> getPageListWtj() {
        return this.baseMapper.selectList(
                new LambdaQueryWrapper<TbJcglPzfz>()
                        .eq(TbJcglPzfz::getXtSjzt, "1") // 唯一固定的条件Sjzt为1
        );
    }


    @Override
    public int addEntity(TbJcglPzfzDto dto) {
        // 根据Pzlx,不能添加的数据的配置类型 是和表中数据的配置类型相同
        TbJcglPzfz item = this.baseMapper.selectOne(
                // 1.获取到表中的其他数据的配置类型【pzlx】,与前段传来的配置类型【pzlx】
                new LambdaQueryWrapper<TbJcglPzfz>()
                        .eq(TbJcglPzfz::getPzlx, dto.getPzlx())
        );
        if (item != null) { // 2.如果前段传来的数据在数据库中有那么就抛异常。
            throw new BusinessException("该配置类型已存在，请重新选择");
        }
        UserInfo user = UserInfoManager.get();
        TbJcglPzfz pzfz = new TbJcglPzfz();
        BeanUtils.copyProperties(dto, pzfz);
        pzfz.setId(UUIDGenerator.getUUID());
        pzfz.setPzrid(String.valueOf(user.getUserId()));
        pzfz.setPzrxm(user.getUserName());
        pzfz.setPzrsfzh(user.getIdEntityCard());
        pzfz.setXtSjly("1");
        return this.baseMapper.insert(pzfz);
    }

    @Override
    public int deleteEntity(String id) {
        return this.baseMapper.deleteByPzfzId(id);
    }

    // 修改
    @Override
    public int editEntity(TbJcglPzfzDto dto) {
        // 根据Pzlx,不能修改成相同的配置类型
        TbJcglPzfz item = this.baseMapper.selectOne(
                new LambdaQueryWrapper<TbJcglPzfz>() // LambdaQueryWrapper对比是否其他的数据有相同的类型
                        .eq(TbJcglPzfz::getPzlx, dto.getPzlx())
                        .ne(TbJcglPzfz::getId, dto.getId()) // LambdaQueryWrapper对比Id是否相等
        );
        if (item != null) { // 如果不为空 直接抛异常
            throw new BusinessException("该配置类型已存在，请重新选择");
        }
        TbJcglPzfz tbJcglPzfz = new TbJcglPzfz();
        BeanUtils.copyProperties(dto, tbJcglPzfz);
        return this.baseMapper.updateById(tbJcglPzfz);
    }

    @Override
    public TbJcglPzfz getInfo(String pzlx) {
        return this.baseMapper.selectPzlx(pzlx);
    }

}
