package org.dromara.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.CommissionSection;
import org.dromara.system.domain.bo.CommissionSectionBo;
import org.dromara.system.domain.vo.CommissionSectionVo;
import org.dromara.system.mapper.CommissionSectionMapper;
import org.dromara.system.service.ICommissionSectionService;
import org.springframework.stereotype.Service;
import java.util.List;
import org.dromara.system.mapper.CommissionTemplateMapper;
import org.dromara.system.domain.CommissionTemplate;
import org.dromara.common.core.exception.ServiceException;

/**
 * 分成比例区间Service实现
 */
@RequiredArgsConstructor
@Service
public class CommissionSectionServiceImpl extends ServiceImpl<CommissionSectionMapper,CommissionSection> implements ICommissionSectionService{
    private final CommissionSectionMapper baseMapper;
    private final CommissionTemplateMapper templateMapper;

    @Override
    public TableDataInfo<CommissionSectionVo> queryPageList(CommissionSectionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CommissionSection> lqw = new LambdaQueryWrapper<>();
        lqw.eq(bo.getTemplateId() != null, CommissionSection::getTemplateId, bo.getTemplateId());
        Page<CommissionSectionVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public CommissionSectionVo queryById(Integer id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public Boolean insertByBo(CommissionSectionBo bo) {
        // 1. 查询模板类型
        CommissionTemplate template = templateMapper.selectById(bo.getTemplateId());
        if (template == null) {
            throw new ServiceException("分成比例模板不存在");
        }
        String templateType = template.getTemplateType();
        // 2. 校验逻辑
        if ("2".equals(templateType)) {
            if (bo.getRateName() != null && !bo.getRateName().isEmpty()) {
                throw new ServiceException("类型为2的模板不能填写名称");
            }
        } else if ("1".equals(templateType)) {
            if (bo.getRateName() == null || bo.getRateName().isEmpty()) {
                throw new ServiceException("类型为1的模板必须填写名称");
            }
        }
        CommissionSection entity = cn.hutool.core.bean.BeanUtil.toBean(bo, CommissionSection.class);
        return baseMapper.insert(entity) > 0;
    }

    @Override
    public Boolean updateByBo(CommissionSectionBo bo) {
        // 1. 查询模板类型
        CommissionTemplate template = templateMapper.selectById(bo.getTemplateId());
        if (template == null) {
            throw new ServiceException("分成比例模板不存在");
        }
        String templateType = template.getTemplateType();
        // 2. 校验逻辑
        if ("2".equals(templateType)) {
            if (bo.getRateName() != null && !bo.getRateName().isEmpty()) {
                throw new ServiceException("类型为2的模板不能填写名称");
            }
        } else if ("".equals(templateType)) {
            if (bo.getRateName() == null || bo.getRateName().isEmpty()) {
                throw new ServiceException("类型为1的模板必须填写名称");
            }
        }
        CommissionSection entity = cn.hutool.core.bean.BeanUtil.toBean(bo, CommissionSection.class);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Integer> ids, Boolean isValid) {
        // 可扩展校验逻辑
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<CommissionSectionVo> queryListByTemplateId(Integer templateId) {
        LambdaQueryWrapper<CommissionSection> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CommissionSection::getTemplateId, templateId);
        List<CommissionSection> list = baseMapper.selectList(lqw);
        // 转VO
        return list.stream().map(section -> cn.hutool.core.bean.BeanUtil.toBean(section, CommissionSectionVo.class)).toList();
    }
}
