package com.bonus.canteen.core.kitchen.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.kitchen.domain.constants.CommonFlagEnum;
import com.bonus.canteen.core.kitchen.domain.constants.SampleDishesSaveStatusEnum;
import com.bonus.canteen.core.kitchen.domain.constants.SampleDishesSearchType;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.kitchen.mapper.KitchenSampleDishesRecordMapper;
import com.bonus.canteen.core.kitchen.domain.KitchenSampleDishesRecord;
import com.bonus.canteen.core.kitchen.service.IKitchenSampleDishesRecordService;

/**
 * 留样机留样清单Service业务层处理
 * 
 * @author xsheng
 * @date 2025-06-16
 */
@Service
public class KitchenSampleDishesRecordServiceImpl implements IKitchenSampleDishesRecordService {
    @Autowired
    private KitchenSampleDishesRecordMapper kitchenSampleDishesRecordMapper;

    /**
     * 查询留样机留样清单
     * 
     * @param recordId 留样机留样清单主键
     * @return 留样机留样清单
     */
    @Override
    public KitchenSampleDishesRecord selectKitchenSampleDishesRecordByRecordId(Long recordId) {
        return kitchenSampleDishesRecordMapper.selectKitchenSampleDishesRecordByRecordId(recordId);
    }

    /**
     * 查询留样机留样清单列表
     * 
     * @param kitchenSampleDishesRecord 留样机留样清单
     * @return 留样机留样清单
     */
    @Override
    public List<KitchenSampleDishesRecord> selectKitchenSampleDishesRecordList(KitchenSampleDishesRecord kitchenSampleDishesRecord) {
        if(kitchenSampleDishesRecord.getSearchType().equals(SampleDishesSearchType.TODAY.getKey())) {
            kitchenSampleDishesRecord.setStartDateTime(LocalDateTime.of(LocalDate.now(),LocalTime.MIDNIGHT));
            kitchenSampleDishesRecord.setEndDateTime(LocalDateTime.of(LocalDate.now(),LocalTime.MAX));
        }else if(kitchenSampleDishesRecord.getSearchType().equals(SampleDishesSearchType.YESTERDAY.getKey())) {
            LocalDate yesterday = LocalDate.now().minusDays(1);
            kitchenSampleDishesRecord.setStartDateTime(LocalDateTime.of(yesterday,LocalTime.MIDNIGHT));
            kitchenSampleDishesRecord.setEndDateTime(LocalDateTime.of(yesterday,LocalTime.MAX));
        }
        List<KitchenSampleDishesRecord> kitchenSampleDishesRecordList = kitchenSampleDishesRecordMapper
                .selectKitchenSampleDishesRecordList(kitchenSampleDishesRecord);
        if(CollUtil.isNotEmpty(kitchenSampleDishesRecordList)) {
            for (KitchenSampleDishesRecord record : kitchenSampleDishesRecordList) {
                record.setSaveStatusName(SampleDishesSaveStatusEnum.getDescByKey(record.getSaveStatus()));
                String illegalStatusName = "";
                if(CommonFlagEnum.YES.getKey().equals(record.getWeightStandard())) {
                    illegalStatusName = illegalStatusName + "重量未达标";
                }
                if(CommonFlagEnum.YES.getKey().equals(record.getDurationStandard())) {
                    illegalStatusName = illegalStatusName + "\n时长未达标";
                }
                if(StringUtils.isBlank(illegalStatusName)) {
                    illegalStatusName = "正常";
                }
                record.setIllegalStatusName(illegalStatusName);
            }
        }
        return kitchenSampleDishesRecordList;
    }

    /**
     * 新增留样机留样清单
     * 
     * @param kitchenSampleDishesRecord 留样机留样清单
     * @return 结果
     */
    @Override
    public int insertKitchenSampleDishesRecord(KitchenSampleDishesRecord kitchenSampleDishesRecord) {
        kitchenSampleDishesRecord.setCreateTime(DateUtils.getNowDate());
        try {
            return kitchenSampleDishesRecordMapper.insertKitchenSampleDishesRecord(kitchenSampleDishesRecord);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改留样机留样清单
     * 
     * @param kitchenSampleDishesRecord 留样机留样清单
     * @return 结果
     */
    @Override
    public int updateKitchenSampleDishesRecord(KitchenSampleDishesRecord kitchenSampleDishesRecord) {
        kitchenSampleDishesRecord.setUpdateTime(DateUtils.getNowDate());
        try {
            return kitchenSampleDishesRecordMapper.updateKitchenSampleDishesRecord(kitchenSampleDishesRecord);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除留样机留样清单
     * 
     * @param recordIds 需要删除的留样机留样清单主键
     * @return 结果
     */
    @Override
    public int deleteKitchenSampleDishesRecordByRecordIds(Long[] recordIds) {
        return kitchenSampleDishesRecordMapper.deleteKitchenSampleDishesRecordByRecordIds(recordIds);
    }

    /**
     * 删除留样机留样清单信息
     * 
     * @param recordId 留样机留样清单主键
     * @return 结果
     */
    @Override
    public int deleteKitchenSampleDishesRecordByRecordId(Long recordId) {
        return kitchenSampleDishesRecordMapper.deleteKitchenSampleDishesRecordByRecordId(recordId);
    }
}
