package com.zsElectric.boot.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zsElectric.boot.business.model.dto.ExclusivePriceDTO;
import com.zsElectric.boot.business.model.query.ExclusivePriceQuery;
import com.zsElectric.boot.business.model.vo.ExclusivePriceVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
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.zsElectric.boot.business.mapper.FirmStationTimePriceMapper;
import com.zsElectric.boot.business.service.FirmStationTimePriceService;
import com.zsElectric.boot.business.model.entity.FirmStationTimePrice;
import com.zsElectric.boot.business.model.form.FirmStationTimePriceForm;
import com.zsElectric.boot.business.model.query.FirmStationTimePriceQuery;
import com.zsElectric.boot.business.model.vo.FirmStationTimePriceVO;
import com.zsElectric.boot.business.converter.FirmStationTimePriceConverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 企业专属价格服务实现类
 *
 * @author zsElectric
 * @since 2025-12-11 10:13
 */
@Service
@RequiredArgsConstructor
public class FirmStationTimePriceServiceImpl extends ServiceImpl<FirmStationTimePriceMapper, FirmStationTimePrice> implements FirmStationTimePriceService {

    private final FirmStationTimePriceConverter firmStationTimePriceConverter;

    /**
    * 获取企业专属价格分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<FirmStationTimePriceVO>} 企业专属价格分页列表
    */
    @Override
    public IPage<FirmStationTimePriceVO> getFirmStationTimePricePage(FirmStationTimePriceQuery queryParams) {
        Page<FirmStationTimePriceVO> pageVO = this.baseMapper.getFirmStationTimePricePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取企业专属价格表单数据
     *
     * @param id 企业专属价格ID
     * @return 企业专属价格表单数据
     */
    @Override
    public FirmStationTimePriceForm getFirmStationTimePriceFormData(Long id) {
        FirmStationTimePrice entity = this.getById(id);
        return firmStationTimePriceConverter.toForm(entity);
    }
    
    /**
     * 新增企业专属价格
     *
     * @param formData 企业专属价格表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveFirmStationTimePrice(FirmStationTimePriceForm formData) {
        FirmStationTimePrice entity = firmStationTimePriceConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新企业专属价格
     *
     * @param id   企业专属价格ID
     * @param formData 企业专属价格表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateFirmStationTimePrice(Long id,FirmStationTimePriceForm formData) {
        FirmStationTimePrice entity = firmStationTimePriceConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除企业专属价格
     *
     * @param ids 企业专属价格ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteFirmStationTimePrices(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的企业专属价格数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public List<ExclusivePriceVO> getExclusivePriceList(ExclusivePriceQuery queryParams) {
        Page<ExclusivePriceDTO> page = baseMapper.getExclusivePriceList(new Page<>(queryParams.getPageNum(),
                queryParams.getPageSize()), queryParams.getStationId(), queryParams.getFirmId());
        List<ExclusivePriceDTO> exclusivePriceList = page.getRecords();
                List<ExclusivePriceVO> priceVOList=new ArrayList<>();

        exclusivePriceList.forEach(priceVO -> {
            ExclusivePriceVO respVO = BeanUtil.toBean(priceVO, ExclusivePriceVO.class);
            respVO.setSaleTotalPrice(NumberUtil.round(NumberUtil.add(priceVO.getEPrice(),priceVO.getAddServicePrice()),2));
            if (ObjectUtil.isEmpty(respVO.getFirmPrice())){
                respVO.setFirmPrice(NumberUtil.round(NumberUtil.add(priceVO.getEPrice(),priceVO.getAddServicePrice()),2));
            }
            priceVOList.add(respVO);
        });
        return priceVOList;
    }

}
