package com.ruoyi.app.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.app.DTO.PorkSideProduceDTO;
import com.ruoyi.app.domain.*;
import com.ruoyi.app.domain.request.ReqGetPorkSideProduce;
import com.ruoyi.app.domain.request.ReqHookRegister;
import com.ruoyi.app.mapper.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.service.IPorkSideProduceService;

import javax.annotation.PostConstruct;

import static com.ruoyi.common.utils.uuid.IdUtils.fastSimpleUUID;

/**
 * 白条生产记录Service业务层处理
 * 
 * @author coede
 * @date 2025-03-19
 */
@Service
public class PorkSideProduceServiceImpl implements IPorkSideProduceService 
{
    @Autowired
    private PorkSideProduceMapper porkSideProduceMapper;

    //待删

//    @Autowired
//    private DistributeBatchMapper distributeBatchMapper;
//
//    @Autowired
//    private HookRegisterMapper hookRegisterMapper;
//
//    @Autowired
//    private EntranceBatchMapper entranceBatchMapper;
//
//    @Autowired
//    private HookMapper hookMapper;
//
//    @PostConstruct
//    private void buildData(){
//        List<DistributeBatch> distributeBatches = distributeBatchMapper.selectDistributeBatchList(new DistributeBatch());
//
//        List<HookRegister> hookRegisters = hookRegisterMapper.selectHookRegisterList(new ReqHookRegister());
//
//        Hook hook = hookMapper.selectDefaultHook();
//
//        for(DistributeBatch item : distributeBatches){
//
//
//            for (int i = 0; i < item.getAmount(); i++) {
//                EntranceBatch entranceBatch = entranceBatchMapper.selectEntranceBatchById(item.getEntranceBatchId());
//                Date bindTime = getRandomTimeBetween11PMAnd4AM(entranceBatch.getEntranceTime());
//                BigDecimal scaleWeight = new BigDecimal(getRandomNumberFormatted());
//
//                PorkSideProduce porkSideProduce = new PorkSideProduce();
//                porkSideProduce.setProductName(PorkSideProduce.DEFAULT_NAME);
//                porkSideProduce.setHookNo(findHookRegister(hookRegisters).getHookNo());
//                porkSideProduce.setHookName(hook.getHookName());
//                porkSideProduce.setProduceTime(bindTime);
//                porkSideProduce.setEntranceBatchId(item.getEntranceBatchId());
//                porkSideProduce.setDistributeBatchId(item.getId());
//                porkSideProduce.setSlaughterCode(item.getSlaughterCode());
//                porkSideProduce.setCreateTime(bindTime);
//                porkSideProduce.setCreateBy(item.getCreateBy());
//                porkSideProduce.setHookWeight(hook.getHookWeight());
//                porkSideProduce.setScaleWeight(scaleWeight);
//                porkSideProduce.setDryLossWeight(BigDecimal.valueOf(0));
//                porkSideProduce.setFinalWeight(scaleWeight.subtract(hook.getHookWeight()));
//                porkSideProduceMapper.insertPorkSideProduce(porkSideProduce);
//            }
//
//        }
//    }
//    public static String getRandomNumberFormatted() {
//        Random random = new Random();
//        double number = 105.0 + (115.0 - 105.0) * random.nextDouble();
//        DecimalFormat df = new DecimalFormat("#.#"); // 保留一位小数
//        return df.format(number);
//    }
//
//    private static Date getRandomTimeBetween11PMAnd4AM(Date inputDate) {
//        Random random = new Random();
//
//        // 将Date转换为LocalDateTime以便操作
//        LocalDateTime date = inputDate.toInstant()
//                .atZone(ZoneId.systemDefault())
//                .toLocalDateTime();
//
//        // 当天晚上10点 (22:00)
//        LocalDateTime start = date.toLocalDate().atTime(23, 0);
//
//        // 次日凌晨3点 (03:00)
//        LocalDateTime end = date.toLocalDate().plusDays(1).atTime(4, 0);
//
//        // 计算两个时间点之间的秒数差
//        long startSeconds = start.atZone(ZoneId.systemDefault()).toEpochSecond();
//        long endSeconds = end.atZone(ZoneId.systemDefault()).toEpochSecond();
//
//        // 生成随机秒数偏移量
//        long randomSeconds = startSeconds + (long)(random.nextDouble() * (endSeconds - startSeconds));
//
//        // 转换为Date
//        return Date.from(Instant.ofEpochSecond(randomSeconds)
//                .atZone(ZoneId.systemDefault())
//                .toInstant());
//    }
//    private HookRegister findHookRegister(List<HookRegister> list){
//        int index = new Random().nextInt(list.size());
//        return list.get(index);
//    }


    /**
     * 查询白条生产记录
     * 
     * @param id 白条生产记录主键
     * @return 白条生产记录
     */
    @Override
    public PorkSideProduceDTO selectPorkSideProduceById(Long id)
    {
        return porkSideProduceMapper.selectPorkSideProduceById(id);
    }

    /**
     * 查询白条生产记录列表
     * 
     * @param porkSideProduce 白条生产记录
     * @return 白条生产记录
     */
    @Override
    public List<PorkSideProduceDTO> selectPorkSideProduceList(ReqGetPorkSideProduce porkSideProduce)
    {
        return porkSideProduceMapper.selectPorkSideProduceList(porkSideProduce);
    }

    /**
     * 新增白条生产记录
     * 
     * @param porkSideProduce 白条生产记录
     * @return 结果
     */
    @Override
    public int insertPorkSideProduce(PorkSideProduce porkSideProduce)
    {
        porkSideProduce.setCreateTime(DateUtils.getNowDate());
        return porkSideProduceMapper.insertPorkSideProduce(porkSideProduce);
    }

    /**
     * 批量新增白条生产记录
     *
     * @param batch 白条生产记录
     * @return 结果
     */
    @Override
    public int insertPorkSideProduceBatch(List<PorkSideProduce> batch)
    {
        return porkSideProduceMapper.insertPorkSideProduceBatch(batch);
    }

    /**
     * 修改白条生产记录
     * 
     * @param porkSideProduce 白条生产记录
     * @return 结果
     */
    @Override
    public int updatePorkSideProduce(PorkSideProduce porkSideProduce)
    {
        porkSideProduce.setUpdateTime(DateUtils.getNowDate());
        return porkSideProduceMapper.updatePorkSideProduce(porkSideProduce);
    }

    /**
     * 批量删除白条生产记录
     * 
     * @param ids 需要删除的白条生产记录主键
     * @return 结果
     */
    @Override
    public int deletePorkSideProduceByIds(Long[] ids)
    {
        return porkSideProduceMapper.deletePorkSideProduceByIds(ids);
    }

    /**
     * 删除白条生产记录信息
     * 
     * @param id 白条生产记录主键
     * @return 结果
     */
    @Override
    public int deletePorkSideProduceById(Long id)
    {
        return porkSideProduceMapper.deletePorkSideProduceById(id);
    }
}
