package cn.iocoder.yudao.module.crm.service.salesperformanceweight;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.salesperformanceweight.vo.SalesPerformanceWeightPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.salesperformanceweight.vo.SalesPerformanceWeightSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.salesperformanceweight.SalesPerformanceWeightDO;
import cn.iocoder.yudao.module.crm.dal.mysql.salesperformanceweight.SalesPerformanceWeightMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 销售业绩权重设置 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class SalesPerformanceWeightServiceImpl implements SalesPerformanceWeightService {

    @Resource
    private SalesPerformanceWeightMapper salesPerformanceWeightMapper;

    @Override
    public Long createSalesPerformanceWeight(SalesPerformanceWeightSaveReqVO createReqVO) {
        // 插入
        SalesPerformanceWeightDO salesPerformanceWeight = BeanUtils.toBean(createReqVO, SalesPerformanceWeightDO.class);
        salesPerformanceWeightMapper.insert(salesPerformanceWeight);
        // 返回
        return salesPerformanceWeight.getId();
    }

    @Override
    public void updateSalesPerformanceWeight(SalesPerformanceWeightSaveReqVO updateReqVO) {
        // 更新
        SalesPerformanceWeightDO updateObj = BeanUtils.toBean(updateReqVO, SalesPerformanceWeightDO.class);
        salesPerformanceWeightMapper.updateById(updateObj);
    }

    @Override
    public void deleteSalesPerformanceWeight(Long id) {
        // 删除
        salesPerformanceWeightMapper.deleteById(id);
    }

    @Override
    public SalesPerformanceWeightDO getSalesPerformanceWeight() {
        List<SalesPerformanceWeightDO> list = salesPerformanceWeightMapper.selectList();
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return new SalesPerformanceWeightDO();
    }

    @Override
    public PageResult<SalesPerformanceWeightDO> getSalesPerformanceWeightPage(SalesPerformanceWeightPageReqVO pageReqVO) {
        return salesPerformanceWeightMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveOrEdit(SalesPerformanceWeightSaveReqVO createReqVO) {
        SalesPerformanceWeightDO salesPerformanceWeight = BeanUtils.toBean(createReqVO, SalesPerformanceWeightDO.class);
        salesPerformanceWeightMapper.insertOrUpdate(salesPerformanceWeight);
    }

}
