package com.ruoyi.web.v2.v1.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.mapper.JsProducePlanMapper;
import com.ruoyi.web.v2.v1.model.JsProducePlan;
import com.ruoyi.web.v2.v1.service.IJsProducePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 * 生产计划登记 服务实现类
 * </p>
 *
 * @author coede
 * @since 2025-10-16
 */
@Service
public class JsProducePlanServiceImpl extends ServiceImpl<JsProducePlanMapper, JsProducePlan> implements IJsProducePlanService {

    @Autowired
    private JsProducePlanMapper producePlanMapper;
    private static final String allowSlaughterNoPre = "TZ";
    @Transactional
    @Override
    public AjaxResult add(JsProducePlan producePlan) {
        JsProducePlan jsProducePlan = producePlanMapper.selectLatestForUpdate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        if (ObjectUtil.isNotEmpty(jsProducePlan) && ObjectUtil.isNotEmpty(jsProducePlan.getAllowSlaughterNo()) && jsProducePlan.getAllowSlaughterNo().substring(2, 10).equals(format)) {
            String lastAllowSlaughterNo = jsProducePlan.getAllowSlaughterNo().replaceAll(allowSlaughterNoPre,"");
            String formattedNumber = StringUtils.incrementString(lastAllowSlaughterNo,8);
            producePlan.setAllowSlaughterNo(allowSlaughterNoPre + formattedNumber);
        } else {
            producePlan.setAllowSlaughterNo(allowSlaughterNoPre + format+String.format("%04d", 1));
        }
        producePlanMapper.insert(producePlan);
        return success();
    }

    @Override
    public AjaxResult edit(JsProducePlan producePlan) {
        producePlanMapper.updateById(producePlan);
        return success();
    }

    @Override
    public AjaxResult delete(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            producePlanMapper.deleteById(s);
        }
        return success();
    }

    @Override
    public AjaxResult page(Integer pageNum, Integer pageSize, String startTime, String endTime, String allowSlaughterNo) {
        Page<JsProducePlan> page = new Page<>(pageNum, pageSize);
        QueryWrapper<JsProducePlan> queryWrapper = new QueryWrapper<>();
        if ("".equals(startTime) || null == startTime) {

        } else {
            queryWrapper.between("produce_time", startTime+" 00:00:00", endTime+" 23:59:59");
        }
        queryWrapper.orderByDesc("produce_time");
        queryWrapper.orderByDesc("produce_time");
        queryWrapper.like(StringUtils.isNotEmpty(allowSlaughterNo),"allow_slaughter_no", allowSlaughterNo);
        return success(producePlanMapper.selectPage(page, queryWrapper));
    }

    @Override
    public AjaxResult listAll() {
        return success(producePlanMapper.selectList(null));
    }

    @Override
    public JsProducePlan getDetail(JsProducePlan jsProducePlan) {
        return producePlanMapper.selectForOne(jsProducePlan);
    }

    //获取最新计划
    @Override
    public JsProducePlan lastPlan() {
        return producePlanMapper.selectForOne(null);
    }
}
