package com.hdkj.lt.bf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.base.exception.BusinessException;
import com.hdkj.lt.bf.entity.dto.LineRateCapacityDTO;
import com.hdkj.lt.bf.entity.vo.LineRateCapacityVO;
import com.hdkj.lt.bf.mapper.LineRateCapacityMapper;
import com.hdkj.lt.bf.service.ILineRateCapacityService;
import com.hdkj.lt.core.bizms.modle.po.LineRateCapacity;
import com.hdkj.lt.utils.security.SqlInjectionUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * <p>
 * 负荷转供线路额定电路台账信息 服务实现类
 * </p>
 *
 * @author luorui
 * @since 2025-06-23
 */
@Service
public class LineRateCapacityServiceImpl extends ServiceImpl<LineRateCapacityMapper, LineRateCapacity> implements ILineRateCapacityService {

    /**
     *
     * @author luorui
     * @date 2025/6/23 11:36
     * @discription 分页查询
     *
     **/
    @Override
    public Page<LineRateCapacityVO> getLineRateCapacityPage(LineRateCapacityDTO lineRateCapacityDTO) {
        Optional.ofNullable(lineRateCapacityDTO).orElseThrow(() -> new BusinessException("请求参数为空"));
        Optional.ofNullable(lineRateCapacityDTO.getPageSize()).orElseThrow(() -> new BusinessException("分页页长pageSize为空"));
        Optional.ofNullable(lineRateCapacityDTO.getPageNum()).orElseThrow(() -> new BusinessException("分页页码pageNum为空"));
        Page<LineRateCapacity> page = new Page(lineRateCapacityDTO.getPageNum(), lineRateCapacityDTO.getPageSize());
        lineRateCapacityDTO.setLineName(SqlInjectionUtil.xxsEscape(lineRateCapacityDTO.getLineName()));
        Page<LineRateCapacityVO> resultVo = baseMapper.getLineRateCapacityPage(page,lineRateCapacityDTO);
        return resultVo;
    }

    /**
     * @author luorui
     * @date 2025/6/23 11:36
     * @discription 单条更新
     **/
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Boolean saveOrUpdateLineRateCapacity(LineRateCapacityDTO lineRateCapacityDTO) {
        Optional.ofNullable(lineRateCapacityDTO).orElseThrow(() -> new BusinessException("请求参数为空"));
        Optional.ofNullable(lineRateCapacityDTO.getLineId()).orElseThrow(() -> new BusinessException("线路编码为空"));
        LineRateCapacity capacity = new LineRateCapacity();
        BeanUtil.copyProperties(lineRateCapacityDTO, capacity);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null == capacity.getId()) {
            if (null != loginUser) {
                capacity.setCreator(SecurityUtils.getUserId());
            }
            capacity.setCreateTime(LocalDateTime.now());
            //新增数据可能会出现重复数据,表上加唯一性索引
            try {
                return this.save(capacity);
            } catch (DataIntegrityViolationException e) {
                //唯一性约束冲突
                throw new BusinessException("数据已存在请重新查询后修改");
            }
        } else {
            if (null != loginUser) {
                capacity.setLastEditor(SecurityUtils.getUserId());
            }
            capacity.setLastTime(LocalDateTime.now());
            return this.updateById(capacity);
        }
    }


}
