package com.njcn.csharmonic.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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 com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csharmonic.mapper.CsHarmonicPlanMapper;
import com.njcn.csharmonic.param.CsHarmonicPlanParam;
import com.njcn.csharmonic.pojo.po.CsHarmonicPlan;
import com.njcn.csharmonic.pojo.po.CsHarmonicPlanLine;
import com.njcn.csharmonic.service.ICsHarmonicPlanLineService;
import com.njcn.csharmonic.service.ICsHarmonicPlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xy
 * @since 2026-04-15
 */
@Service
@RequiredArgsConstructor
public class CsHarmonicPlanServiceImpl extends ServiceImpl<CsHarmonicPlanMapper, CsHarmonicPlan> implements ICsHarmonicPlanService {

    private final ICsHarmonicPlanLineService csHarmonicPlanLineService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CsHarmonicPlanParam param) {
        CsHarmonicPlan plan = new CsHarmonicPlan();
        plan.setName(param.getName());
        plan.setHarmonicTarget(param.getHarmonicTarget());
        plan.setSort(param.getSort() != null ? param.getSort() : 0);
        this.save(plan);

        if (CollUtil.isNotEmpty(param.getLineList())) {
            for (String lineId : param.getLineList()) {
                LambdaQueryWrapper<CsHarmonicPlanLine> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(CsHarmonicPlanLine::getLineId, lineId);
                csHarmonicPlanLineService.remove(wrapper);

                CsHarmonicPlanLine planLine = new CsHarmonicPlanLine();
                planLine.setId(plan.getId());
                planLine.setLineId(lineId);
                csHarmonicPlanLineService.save(planLine);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CsHarmonicPlanParam.UpdateCsHarmonicPlanParam param) {
        CsHarmonicPlan plan = this.getById(param.getId());
        if (plan == null) {
            throw new RuntimeException("稳态指标方案不存在");
        }
        plan.setName(param.getName());
        plan.setHarmonicTarget(param.getHarmonicTarget());
        plan.setSort(param.getSort() != null ? param.getSort() : plan.getSort());
        this.updateById(plan);

        if (CollUtil.isNotEmpty(param.getLineList())) {
            LambdaQueryWrapper<CsHarmonicPlanLine> oldWrapper = new LambdaQueryWrapper<>();
            oldWrapper.eq(CsHarmonicPlanLine::getId, param.getId());
            csHarmonicPlanLineService.remove(oldWrapper);

            for (String lineId : param.getLineList()) {
                LambdaQueryWrapper<CsHarmonicPlanLine> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(CsHarmonicPlanLine::getLineId, lineId);
                csHarmonicPlanLineService.remove(wrapper);

                CsHarmonicPlanLine planLine = new CsHarmonicPlanLine();
                planLine.setId(param.getId());
                planLine.setLineId(lineId);
                csHarmonicPlanLineService.save(planLine);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteWithLines(List<String> ids) {
        this.removeByIds(ids);
        if (CollUtil.isNotEmpty(ids)) {
            LambdaQueryWrapper<CsHarmonicPlanLine> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(CsHarmonicPlanLine::getId, ids);
            List<CsHarmonicPlanLine> list = csHarmonicPlanLineService.list();
            if (CollUtil.isNotEmpty(list)) {
                throw new BusinessException("该方案下存在监测点,重新分配后再尝试删除!");
            }
        }
    }

    @Override
    public Page<CsHarmonicPlan> getPage(CsHarmonicPlanParam.QueryParam param) {
        Page<CsHarmonicPlan> page = new Page<>(param.getPageNum(), param.getPageSize());
        LambdaQueryWrapper<CsHarmonicPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(param.getName()), CsHarmonicPlan::getName, param.getName())
                .orderByAsc(CsHarmonicPlan::getSort)
                .orderByAsc(CsHarmonicPlan::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public List<CsHarmonicPlan> listAllOrderBySort() {
        LambdaQueryWrapper<CsHarmonicPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(CsHarmonicPlan::getSort);
        return this.list(wrapper);
    }

    @Override
    public CsHarmonicPlan getByIdWithLines(String id) {
        CsHarmonicPlan plan = this.getById(id);
        if (plan != null) {
            List<CsHarmonicPlanLine> planLines = csHarmonicPlanLineService.getByPlanId(id);
            if (CollUtil.isNotEmpty(planLines)) {
                List<String> lineIds = planLines.stream()
                        .map(CsHarmonicPlanLine::getLineId)
                        .collect(Collectors.toList());
                plan.setLineList(lineIds);
            }
        }
        return plan;
    }

    @Override
    public List<CsHarmonicPlan> getByName(String name) {
        LambdaQueryWrapper<CsHarmonicPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CsHarmonicPlan::getName, name);
        return this.list(wrapper);
    }
}
