package com.jwipc.order.controller.pc;

import com.alibaba.fastjson2.JSON;
import com.jwipc.order.dto.AppointmentPurchaseListDTO;
import com.jwipc.order.entity.AppointmentPurchase;
import com.jwipc.order.logic.AppointmentPurchaseLogic;
import com.jwipc.order.model.query.AppointmentPurchaseQuery;
import com.jwipc.order.model.query.AppointmentRecordQuery;
import com.jwipc.page.PageView;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.jwipc.order.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 低值供应商采购类预约
 * </p>
 *
 * @author wt
 * @version 1.0
 * @since 2024/3/6
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(MODULE_ROOT + "/appointmentPurchase/rest")
public class AppointmentPurchaseRestController {

    private final AppointmentPurchaseLogic appointmentPurchaseLogic;
    /**
     * 低值供应商采购类预约 列表加载
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(AppointmentPurchaseQuery query) {
        PageView pageView = appointmentPurchaseLogic.selectListByPage(query);
        return pageView;
    }

    /**
     * 保存or变更预约时间
     *
     * @param appointmentTimeId    预约的时间id
     * @param appointmentDate      预约日期
     * @param appointmentRecordIds 预约记录的ids  有值则是变更 无值则是新增
     * @param detail               明细数据
     * @return
     */
    @PostMapping("saveOrUpdate")
    public ResponseEntity<?> saveOrUpdate(long appointmentTimeId, String appointmentDate, String appointmentRecordIds, String detail) {
        List<AppointmentPurchaseListDTO> appointmentPurchases = JSON.parseArray(detail, AppointmentPurchaseListDTO.class);
        appointmentPurchaseLogic.saveOrUpdate(appointmentTimeId, appointmentDate, appointmentRecordIds, appointmentPurchases);
        return ResponseEntity.ok("ok");
    }
}
