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.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.app.DTO.PorkOtherProduceDTO;
import com.ruoyi.app.domain.*;
import com.ruoyi.app.domain.request.ReqGetPorkOtherProduce;
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.IPorkOtherProduceService;

import javax.annotation.PostConstruct;

/**
 * 猪头生产记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class PorkOtherProduceServiceImpl implements IPorkOtherProduceService 
{
    @Autowired
    private PorkOtherProduceMapper porkOtherProduceMapper;

    //待删

//    @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());
//
//        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());
//
//                PorkOtherProduce porkOtherProduce = new PorkOtherProduce();
//                porkOtherProduce.setProductName("猪头");
//                porkOtherProduce.setProduceTime(bindTime);
//                porkOtherProduce.setEntranceBatchId(item.getEntranceBatchId());
//                porkOtherProduce.setDistributeBatchId(item.getId());
//                porkOtherProduce.setSlaughterCode(item.getSlaughterCode());
//                porkOtherProduce.setCreateTime(bindTime);
//                porkOtherProduce.setCreateBy(item.getCreateBy());
//                porkOtherProduce.setScaleWeight(scaleWeight);
//                porkOtherProduce.setFinalWeight(scaleWeight);
//                porkOtherProduceMapper.insertPorkOtherProduce(porkOtherProduce);
//            }
//
//        }
//    }
//    public static String getRandomNumberFormatted() {
//        Random random = new Random();
//        double number = 55.0 + (55.0 - 45.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());
//    }

    /**
     * 查询生产记录
     * 
     * @param id 生产记录主键
     * @return 生产记录
     */
    @Override
    public PorkOtherProduceDTO selectPorkOtherProduceById(Long id)
    {
        return porkOtherProduceMapper.selectPorkOtherProduceById(id);
    }

    /**
     * 查询生产记录列表
     * 
     * @param porkOtherProduce 生产记录
     * @return 生产记录
     */
    @Override
    public List<PorkOtherProduceDTO> selectPorkOtherProduceList(ReqGetPorkOtherProduce porkOtherProduce)
    {
        return porkOtherProduceMapper.selectPorkOtherProduceList(porkOtherProduce);
    }

    /**
     * 新增生产记录
     * 
     * @param porkOtherProduce 生产记录
     * @return 结果
     */
    @Override
    public int insertPorkOtherProduce(PorkOtherProduce porkOtherProduce)
    {
        porkOtherProduce.setCreateTime(DateUtils.getNowDate());
        return porkOtherProduceMapper.insertPorkOtherProduce(porkOtherProduce);
    }

    /**
     * 修改生产记录
     * 
     * @param porkOtherProduce 生产记录
     * @return 结果
     */
    @Override
    public int updatePorkOtherProduce(PorkOtherProduce porkOtherProduce)
    {
        porkOtherProduce.setUpdateTime(DateUtils.getNowDate());
        return porkOtherProduceMapper.updatePorkOtherProduce(porkOtherProduce);
    }

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

    /**
     * 删除猪头生产记录信息
     * 
     * @param id 生产记录主键
     * @return 结果
     */
    @Override
    public int deletePorkOtherProduceById(Long id)
    {
        return porkOtherProduceMapper.deletePorkOtherProduceById(id);
    }
}
