package com.jwipc.order.logic.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.order.convert.AppointmentPurchaseConvert;
import com.jwipc.order.dto.AppointmentPurchaseBaseDTO;
import com.jwipc.order.dto.AppointmentPurchaseListDTO;
import com.jwipc.order.entity.AppointmentPurchase;
import com.jwipc.order.entity.AppointmentRecord;
import com.jwipc.order.entity.AppointmentTime;
import com.jwipc.order.logic.AppointmentPurchaseLogic;
import com.jwipc.order.model.po.AppointmentPurchasePO;
import com.jwipc.order.model.query.AppointmentPurchaseQuery;
import com.jwipc.order.service.IAppointmentPurchaseService;
import com.jwipc.order.service.IAppointmentRecordService;
import com.jwipc.order.service.IAppointmentTimeService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.sys.client.SequenceClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *
 * </p>
 *
 * @author wt
 * @version 1.0
 * @since 2024/3/6
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AppointmentPurchaseLogicImpl implements AppointmentPurchaseLogic {

    private final IAppointmentPurchaseService appointmentPurchaseService;
    private final AppointmentPurchaseConvert appointmentPurchaseConvert;
    private final SequenceClient sequenceClient;
    private final IAppointmentTimeService appointmentTimeService;
    private final IAppointmentRecordService appointmentRecordService;
    @Override
    public List<AppointmentPurchaseListDTO> selectDataByQuery(AppointmentPurchaseQuery query) {
        List<AppointmentPurchasePO> poList = appointmentPurchaseService.selectListByPage(null, query);
        return appointmentPurchaseConvert.POToDTOList(poList);
    }

    @Override
    public PageView selectListByPage(AppointmentPurchaseQuery query) {
        final Page<AppointmentPurchase> dbPage = PageUtil.toPage(query);
        List<AppointmentPurchasePO> poList = appointmentPurchaseService.selectListByPage(dbPage,query);
        //todo 查询未清数量 暂时没有逻辑

        List<AppointmentPurchaseListDTO> dtoList = appointmentPurchaseConvert.POToDTOList(poList);
        return PageUtil.getPV(dtoList, dbPage.getTotal());
    }

    /**
     * 保存预约采购数据
     * @param appointmentTimeId 预约的时间id
     * @param appointmentDate 预约日期
     * @param appointmentRecordIds 预约记录的ids  有值则是变更 无值则是新增
     * @param appointmentPurchases 明细数据
     */
    @Override
    public void saveOrUpdate(long appointmentTimeId, String appointmentDate, String appointmentRecordIds, List<AppointmentPurchaseListDTO> appointmentPurchases) {
        AppointmentTime appointmentTime = appointmentTimeService.getById(appointmentTimeId);

        if (StringUtils.isEmpty(appointmentRecordIds)) { //新增
            AppointmentPurchaseBaseDTO appointmentPurchase = appointmentPurchases.get(0);
            AppointmentRecord record = new AppointmentRecord();
            record.setType( 2);
            record.setDockerCode(appointmentTime.getDockerCode());
            record.setFactory(appointmentTime.getFactoryCode());
            record.setStatus( 1);
//            record.setAppointmentCode(sequenceClient.createSequenceNo("appointment_code"));
            record.setAppointmentCode(sequenceClient.getBarCodeSequenceNo("appointment_code", 4, false));
            record.setSupplierCode(appointmentPurchase.getSupplierCode());
            record.setSupplierName(appointmentPurchase.getSupplierName());
//            record.setMaxNum(appointmentTime.getMaxNum());
            record.setAppointmentTimeId(appointmentTimeId);
            record.setAppointmentStartTime(appointmentTime.getStartTime());
            record.setAppointmentEndTime(appointmentTime.getEndTime());
            record.setAppointmentDate(LocalDateTimeUtil.parseDate(appointmentDate));
            record.setCreateTime(LocalDateTime.now());
            record.setUrgencyFlag(0);
            appointmentRecordService.save(record);
            appointmentPurchases.forEach(t -> t.setAppointmentRecordId(record.getId()));
            List<AppointmentPurchase> appointmentPurchaseList = appointmentPurchaseConvert.dtoToEntityList(appointmentPurchases);
            appointmentPurchaseService.saveOrUpdateBatch(appointmentPurchaseList);
        } else { //编辑
            List<String> ids = Arrays.asList(appointmentRecordIds.split(","));
            List<AppointmentRecord> records = appointmentRecordService.listByIds(ids);
            records.forEach(t -> {
                Long oldAppointmentId = t.getAppointmentTimeId();
                LocalDate oldeAppointmentDate = t.getAppointmentDate();
                String oldStart = t.getAppointmentStartTime();
                String oldEnd = t.getAppointmentEndTime();
                t.setAppointmentDate(LocalDateTimeUtil.parseDate(appointmentDate));
                t.setAppointmentTimeId(appointmentTimeId);
                t.setAppointmentStartTime(appointmentTime.getStartTime());
                t.setAppointmentEndTime(appointmentTime.getEndTime());
                t.setDockerCode(appointmentTime.getDockerCode());
                t.setFactory(appointmentTime.getFactoryCode());
                t.setLastAppointmentId(oldAppointmentId);
                t.setLastDate(oldeAppointmentDate);
                t.setLastStartTime(oldStart);
                t.setLastEndTime(oldEnd);
                t.setUpdateTime(LocalDateTime.now());
            });
            appointmentRecordService.saveOrUpdateBatch(records);
        }
    }
}
