package cn.cslg.pas.service.impl;

import cn.cslg.pas.common.model.PersonnelVO;
import cn.cslg.pas.common.model.QueryPREntity;
import cn.cslg.pas.common.model.dto.PermissionRecordDTO;
import cn.cslg.pas.common.model.dto.PermissionRecordUpdateDTO;
import cn.cslg.pas.common.model.vo.PermissionRecordVO;
import cn.cslg.pas.common.utils.CacheUtils;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.domain.PermissionRecord;
import cn.cslg.pas.exception.XiaoShiException;
import cn.cslg.pas.mapper.PermissionRecordMapper;
import cn.cslg.pas.service.IPermissionRecordService;
import com.github.pagehelper.PageHelper;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * 许可记录的Service层实现类
 *
 * @Author xiexiang
 * @Date 2023/3/9
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PermissionRecordServiceImpl implements IPermissionRecordService {
    private final PermissionRecordMapper permissionRecordMapper; //许可记录表的Mapper层装配
    private final CacheUtils cacheUtils;
    private final LoginUtils loginUtils;

    /**
     * 新增许可记录
     *
     * @param permissionRecordDTO 新增许可记录的前端传输DTO数据对象
     */
    @Override
    public void add(PermissionRecordDTO permissionRecordDTO) {
        log.info("开始处理【新增许可记录】的业务，参数为：{}", permissionRecordDTO);

        //DTO赋值给许可记录表实体类
        PermissionRecord permissionRecord = new PermissionRecord();
        BeanUtils.copyProperties(permissionRecordDTO, permissionRecord);
        //获取当前登陆人信息
        PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
        //给实体类赋值登陆人id和姓名
        permissionRecord.setCreatePersonId(personnelVO.getId());
        permissionRecord.setCreatePersonName(personnelVO.getName());
        permissionRecord.setTenantId(personnelVO.getTenantId());
        //数据入表
        log.info("数据入表");
        int rows = permissionRecordMapper.add(permissionRecord);
        if (rows != 1) {
            String message = "新增许可记录失败，请稍后再试";
            log.info("数据入许可记录表失败，{}", message);
            throw new XiaoShiException(message);
        }
        log.info("新增许可记录完成");
    }

    /**
     * 修改许可记录
     *
     * @param permissionRecordUpdateDTO 修改许可记录的前端传输DTO数据对象
     */
    @Override
    public void update(PermissionRecordUpdateDTO permissionRecordUpdateDTO) {
        log.info("开始处理【修改许可记录】的业务，参数为：{}", permissionRecordUpdateDTO);
        //DTO赋值给许可记录表实体类
        PermissionRecord permissionRecord = new PermissionRecord();
        BeanUtils.copyProperties(permissionRecordUpdateDTO, permissionRecord);

        //根据id修改许可记录表数据
        log.info("修改许可记录表数据");
        int rows = permissionRecordMapper.update(permissionRecord);
        if (rows != 1) {
            String message = "修改许可记录失败，服务器忙请稍后再试";
            log.info("修改许可记录表数据失败，{}", message);
        }
        log.info("许可记录修改完成");
    }

    /**
     * 根据专利号patentNo、当前页current、每页条数size查询许可记录数据
     *
     * @param
     * @return 返回查询到数据
     */
    @Override
    public List<PermissionRecordVO> queryAllInfo(String patentNo) {
        PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
        Integer tenantId = personnelVO.getTenantId();
        return permissionRecordMapper.queryAllInfo(patentNo, tenantId);
    }

    /**
     * 分页查询
     */
    @Override
    public List<PermissionRecordVO> findAllByPage(QueryPREntity queryPREntity) {
        PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
        Integer tenantId = personnelVO.getTenantId();
        PageHelper.startPage(queryPREntity.getCurrent(), queryPREntity.getSize());
        List<PermissionRecordVO> lists = permissionRecordMapper.queryAllInfo(queryPREntity.getPatentNo(), tenantId);
        return lists;
    }

    @Override
    public Integer countByPatentNo(String patentNo) {
        PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
        Integer tenantId = personnelVO.getTenantId();
        return permissionRecordMapper.countByPatentNo(patentNo, tenantId);
    }


    /**
     * 根据id删除许可记录数据
     *
     * @param id 许可id
     */
    @Override
    public void delete(Integer id) {
        log.info("开始处理【删除许可记录】的业务，参数为：{}", id);
        if (id != null) {
            permissionRecordMapper.delete(id);
            log.info("产品删除完成");
        } else {
            log.info("删除产品失败，产品不存在");
        }
    }
}