package com.ruoyi.project.business.service.impl;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.business.domain.TpAreaBasicPrice;
import com.ruoyi.project.business.domain.TpAreaOptionalPara;
import com.ruoyi.project.business.domain.TpAreaOptionalParaPrice;
import com.ruoyi.project.business.mapper.TpAreaBasicPriceMapper;
import com.ruoyi.project.business.mapper.TpAreaOptionalParaMapper;
import com.ruoyi.project.business.service.ITpAreaOptionalParaPriceService;
import com.ruoyi.project.business.service.ITpAreaOptionalParaService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static cn.hutool.core.text.CharSequenceUtil.isNotBlank;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-28
 */
@Service
public class TpAreaOptionalParaServiceImpl extends ServiceImpl<TpAreaOptionalParaMapper, TpAreaOptionalPara> implements ITpAreaOptionalParaService
{
    @Autowired
    private TpAreaOptionalParaMapper tpAreaOptionalParaMapper;
    @Resource
    private ITpAreaOptionalParaPriceService tpAreaOptionalParaPriceService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TpAreaOptionalPara selectTpAreaOptionalParaById(Long id)
    {
        TpAreaOptionalPara result = tpAreaOptionalParaMapper.selectTpAreaOptionalParaById(id);
        if (isNotBlank(result.getExportParaCodes())){
            result.setExportParaCodeList(Arrays.stream(result.getExportParaCodes().split(",")).collect(Collectors.toList()));
        }
        return result;
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tpAreaOptionalPara 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TpAreaOptionalPara> selectTpAreaOptionalParaList(TpAreaOptionalPara tpAreaOptionalPara)
    {
        List<TpAreaOptionalPara> result = tpAreaOptionalParaMapper.selectTpAreaOptionalParaList(tpAreaOptionalPara);

        List<Long> ids = result.stream().map(TpAreaOptionalPara::getId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(ids)) {
            TpAreaOptionalParaPrice param = new TpAreaOptionalParaPrice();
            param.setMarkupIds(ids);
            List<TpAreaOptionalParaPrice> tpAreaOptionalParaPrices = tpAreaOptionalParaPriceService.selectTpAreaOptionalParaPriceList(param);
            Map<Long, List<TpAreaOptionalParaPrice>> detailMap = tpAreaOptionalParaPrices.stream().collect(Collectors.groupingBy(TpAreaOptionalParaPrice::getMarkupId));
            result.forEach(item -> {
                List<TpAreaOptionalParaPrice> detailList = detailMap.get(item.getId());
                if (CollectionUtils.isNotEmpty(detailList)) {
                    item.setOptionCount(detailMap.get(item.getId()).size());
                } else {
                    item.setOptionCount(0);
                }
            });
        }

        return result;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tpAreaOptionalPara 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTpAreaOptionalPara(TpAreaOptionalPara tpAreaOptionalPara)
    {
        tpAreaOptionalPara.setId(null);
        checkParam(tpAreaOptionalPara);
        tpAreaOptionalPara.setStatus("0");
        tpAreaOptionalPara.setCreateTime(DateUtils.getNowDate());
        if (CollectionUtils.isNotEmpty(tpAreaOptionalPara.getExportParaCodeList())){
            tpAreaOptionalPara.setExportParaCodes(tpAreaOptionalPara.getExportParaCodeList().stream().collect(Collectors.joining(",")));
        } else {
            tpAreaOptionalPara.setExportParaCodes("");
        }
        return tpAreaOptionalParaMapper.insertTpAreaOptionalPara(tpAreaOptionalPara);
    }

    private void checkParam(TpAreaOptionalPara tpAreaOptionalPara) {
        List<String> params = Lists.newArrayList(tpAreaOptionalPara.getParaCode(), tpAreaOptionalPara.getRelatedParaCode1(), tpAreaOptionalPara.getRelatedParaCode2(), tpAreaOptionalPara.getRelatedParaCode3(),
                tpAreaOptionalPara.getRelatedParaCode4(), tpAreaOptionalPara.getRelatedParaCode5(), tpAreaOptionalPara.getRelatedParaCode6(),
                tpAreaOptionalPara.getRelatedParaCode7(), tpAreaOptionalPara.getRelatedParaCode8(), tpAreaOptionalPara.getRelatedParaCode9(),
                tpAreaOptionalPara.getRelatedParaCode10()).stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
        if (params.size() > params.stream().distinct().count()) {
            throw new ServiceException("关联参数不能重复");
        }
        if (baseMapper.exists(Wrappers.<TpAreaOptionalPara>lambdaQuery()
                .eq(TpAreaOptionalPara::getElevatorModelId, tpAreaOptionalPara.getElevatorModelId())
                .eq(TpAreaOptionalPara::getMarkupName, tpAreaOptionalPara.getMarkupName())
                .ne(Objects.nonNull(tpAreaOptionalPara.getId()), TpAreaOptionalPara::getId, tpAreaOptionalPara.getId())
        )) {
            throw new ServiceException("加价项目名称不能重复");
        }
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tpAreaOptionalPara 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTpAreaOptionalPara(TpAreaOptionalPara tpAreaOptionalPara)
    {
        checkParam(tpAreaOptionalPara);
        tpAreaOptionalPara.setUpdateTime(DateUtils.getNowDate());
        if (CollectionUtils.isNotEmpty(tpAreaOptionalPara.getExportParaCodeList())){
            tpAreaOptionalPara.setExportParaCodes(tpAreaOptionalPara.getExportParaCodeList().stream().collect(Collectors.joining(",")));
        } else {
            tpAreaOptionalPara.setExportParaCodes("");
        }

        tpAreaOptionalParaPriceService.update(Wrappers.<TpAreaOptionalParaPrice>lambdaUpdate()
                .set(TpAreaOptionalParaPrice::getMarkupName, tpAreaOptionalPara.getMarkupName())
                .eq(TpAreaOptionalParaPrice::getElevatorModelId, tpAreaOptionalPara.getElevatorModelId())
                .eq(TpAreaOptionalParaPrice::getMarkupId, tpAreaOptionalPara.getId()));

        return tpAreaOptionalParaMapper.updateTpAreaOptionalPara(tpAreaOptionalPara);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTpAreaOptionalParaByIds(Long[] ids)
    {
        if (CollectionUtils.isEmpty(listByIds(Arrays.asList(ids)))) {
            throw new ServiceException("数据不存在或已被删除，请刷新后重试");
        }
        tpAreaOptionalParaPriceService.remove(Wrappers.<TpAreaOptionalParaPrice>lambdaQuery().in(TpAreaOptionalParaPrice::getMarkupId, ids));
        return tpAreaOptionalParaMapper.deleteTpAreaOptionalParaByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTpAreaOptionalParaById(Long id)
    {
        if (Objects.isNull(getById(id))) {
            throw new ServiceException("数据不存在或已被删除，请刷新后重试");
        }
        return tpAreaOptionalParaMapper.deleteTpAreaOptionalParaById(id);
    }

    @Override
    public void deleteTpAreaOptionalParaByElevatorModelId(Long id) {
        tpAreaOptionalParaMapper.deleteTpAreaOptionalParaByElevatorModelId(id);
    }

    @Override
    public List<TpAreaOptionalPara> getOptionParaSelection(Long id) {
        TpAreaOptionalPara current = tpAreaOptionalParaMapper.selectTpAreaOptionalParaById(id);

        TpAreaOptionalPara para = new TpAreaOptionalPara();
        para.setElevatorModelId(current.getElevatorModelId());
        List<TpAreaOptionalPara> list = tpAreaOptionalParaMapper.selectTpAreaOptionalParaList(para);

        return list.stream().filter(item ->item.getSortIndex() < current.getSortIndex()).collect(Collectors.toList());
    }
}
