package com.fizz.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fizz.business.domain.CrmPdiPlan;
import com.fizz.business.form.ChangePlanStatusForm;
import com.fizz.business.form.PlanQueryForm;
import com.fizz.business.mapper.CrmPdiPlanMapper;
import com.fizz.business.service.CrmPdiPlanService;
import com.fizz.business.vo.CrmPdiPlanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class CrmPdiPlanServiceImpl extends ServiceImpl<CrmPdiPlanMapper, CrmPdiPlan> implements CrmPdiPlanService {


    /**
     * 根据卷ID和操作员ID查询单个记录
     *
     * @param coilid 卷ID
     * @return 查询到的CrmPdiPlan对象
     */
    public CrmPdiPlanVO getByCoilIdAndOperId(String coilid) {

        QueryWrapper<CrmPdiPlan> queryWrapper = new QueryWrapper<>();
        if (coilid.equalsIgnoreCase("ONLINE")) {
            queryWrapper.eq("STATUS", coilid);
            CrmPdiPlan one = this.getOne(queryWrapper);
            return BeanUtil.copyProperties(one, CrmPdiPlanVO.class);

        }else {
            queryWrapper.like("coilid", coilid);
            CrmPdiPlan one = this.getOne(queryWrapper);

            return BeanUtil.copyProperties(one, CrmPdiPlanVO.class);
        }

    }

    private CrmPdiPlan getByCoilId(String coilid) {

        LambdaQueryWrapper<CrmPdiPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CrmPdiPlan::getCoilid,coilid);

        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 添加新记录
     *
     * @param crmPdiPlan 要添加的CrmPdiPlan对象
     * @return 是否添加成功
     */
    public boolean addCrmPdiPlan(CrmPdiPlan crmPdiPlan) {
          crmPdiPlan.setStatus("READY");

        return this.save(crmPdiPlan);
    }

    /**
     * 更新已有记录
     *
     * @param crmPdiPlan 要更新的CrmPdiPlan对象
     * @return 是否更新成功
     */
    public boolean updateCrmPdiPlan(CrmPdiPlan crmPdiPlan) {
        return this.updateById(crmPdiPlan);
    }

    /**
     * 根据卷ID和操作员ID删除记录
     *
     * @param coilid 卷ID
     * @return 是否删除成功
     */
    public boolean deleteCrmPdiPlan(List<Long> coilid) {
        QueryWrapper<CrmPdiPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("COILID", coilid);
        return this.removeBatchByIds(coilid);
    }

    /**
     * 查询所有记录
     *
     * @param form 查询条件
     * @return CrmPdiPlan对象的列表
     */
    public List<CrmPdiPlanVO> listAll(PlanQueryForm form) {
        QueryWrapper<CrmPdiPlan> queryWrapper = new QueryWrapper<>();

        // 卷ID
        if (StrUtil.isNotBlank(form.getCoilid())) {
            queryWrapper.eq("coilid", form.getCoilid());
        }

        // 入口厚度
        if (form.getEntryThick() != null) {
            queryWrapper.eq("entry_thick", form.getEntryThick());
        }

        // 入口宽度
        if (form.getEntryWidth() != null) {
            queryWrapper.eq("entry_width", form.getEntryWidth());
        }

        // 状态
        if (StrUtil.isNotBlank(form.getStatus())) {
            queryWrapper.eq("status", form.getStatus());
        } else {
            // 如果没传状态，默认查 NEW 和 READY
            queryWrapper.in("status", "NEW", "READY");
        }

        // 开始日期
        if (form.getStartDate() != null) {
            queryWrapper.ge("start_date", form.getStartDate());
        }

        // 结束日期
        if (form.getEndDate() != null) {
            queryWrapper.le("end_date", form.getEndDate());
        }

        // 查询并转成 VO
        return BeanUtil.copyToList(this.list(queryWrapper), CrmPdiPlanVO.class);
    }

    @Override
    public CrmPdiPlan getFirstUnProducedCoil() {
        return this.lambdaQuery()
                .in(CrmPdiPlan::getStatus, "NEW","READY") // 0 表示未生产
                .orderByAsc(CrmPdiPlan::getSeqid) // 按顺序号排序，取第一个
                .last("limit 1")
                .one();
    }

    @Override
    public void changeStatus(ChangePlanStatusForm build) {

        QueryWrapper<CrmPdiPlan> wrapper = new QueryWrapper<>();
        wrapper.eq("coilid", build.getMatId());
        wrapper.eq("planid", build.getId());

        CrmPdiPlan pdiPlan = baseMapper.selectOne(wrapper);

        if (pdiPlan == null) {
            log.error("未找到ID为{}的计划记录", build.getId());
            return;
        }

        pdiPlan.setStatus(build.getOperation());

        baseMapper.updateById(pdiPlan);
        log.info("计划状态更新成功，ID: {}, 新状态: {}", build.getId(), build.getOperation());
    }

}
