package com.jwipc.order.controller.pc;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.dto.ExportDataDTO;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.order.dto.*;
import com.jwipc.order.logic.PurchaseOrderLogic;
import com.jwipc.order.model.query.OrderBatchPrintQuery;
import com.jwipc.order.model.query.PurchaseOrderQuery;
import com.jwipc.page.PageView;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

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

/**
 * <p>
 * 采购订单查询 接口
 * </p>
 *
 * @author wt
 * @version 1.0
 * @since 2024/3/4
 */
@RestController
@RequestMapping(MODULE_ROOT + "/purchaseOrder/rest")
@RequiredArgsConstructor
@Slf4j
public class PurchaseOrderRestController {
    private final PurchaseOrderLogic purchaseOrderLogic;

    /**
     * 采购订单列表接口
     *
     * @param query 查询参数
     * @return
     */
    @AuditLogInter(value = "查询", module = "采购订单查询")
    @GetMapping("list")
    public PageView list(PurchaseOrderQuery query) {
        PageView pageView = purchaseOrderLogic.getList(query);
        return pageView;
    }
    /**
     * 查询订单数据
     *
     * @return
     */
    @PostMapping("/order/list")
    public PageView orderList(PurchaseOrderQuery query) {
        PageView pageView = purchaseOrderLogic.orderList(query);
        return pageView;
    }

    /**
     * 清空打印标记
     *
     * @return
     */
    @PostMapping("/clearBatchNo")
    public ResponseEntity clearBatchNo(String batchNo) {
        purchaseOrderLogic.clearBatchNo(batchNo);
        return ResponseEntity.ok().build();
    }



    /**
     * 查询订单数据
     *
     * @return
     */
    @GetMapping("/batchPrint/list")
    public PageView batchPrintList(OrderBatchPrintQuery query) {
        PageView pageView = purchaseOrderLogic.batchPrintList(query);
        return pageView;
    }

    @GetMapping("/batchPrint/listCheck")
    public ResponseEntity batchPrintListCheck(String orderIds, String range) {
        return ResponseEntity.ok(purchaseOrderLogic.batchPrintListCheck(orderIds,range));
    }

    @GetMapping("/getPurchaseOrderById")
    public ResponseEntity getPurchaseOrderById(Long orderId) {
        return ResponseEntity.ok(purchaseOrderLogic.getDetail(orderId));
    }

    /**
     * 采购订单导出接口
     *
     * @return
     */
    @PostMapping("export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<PurchaseOrderListDTO> exportData = purchaseOrderLogic.listAll(
                JSON.parseObject(queryStr, PurchaseOrderQuery.class));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        for (PurchaseOrderListDTO exportDatum : exportData) {
            if(StringUtils.isNotEmpty(exportDatum.getOrderDate())){
                LocalDate date = LocalDate.parse(exportDatum.getOrderDate(), formatter);
                exportDatum.setOrderDate(date.format(DateTimeFormatter.ISO_DATE));
            }
        }
        //需要自己往里面增加
        List<ExportDataDTO> exportDataDTOS = JSON.parseArray(exportFiled, ExportDataDTO.class);
        final ExportDataDTO exportDataDTO = ExportDataDTO.builder().field("id").title("").build();
        exportDataDTOS.add(exportDataDTO);
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,
                "订单数据-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN), "sheet", exportData,
                exportDataDTOS);
    }

    //生成汇总订单pdf文件到前台

//    @PostMapping("batchPrintPdfCreate")
//    public void batchPrintPdfCreate(HttpServletResponse response, String orderIds, String range,String batchNo) {
//        PurchaseOrderPrintDTO batchPrintData = purchaseOrderLogic.getBatchPrintData(orderIds, range, batchNo);
//        purchaseOrderLogic.batchPrintPdfCreate(batchPrintData,response);
//    }

    @PostMapping("batchPrintPdfCreate")
    public ResponseEntity batchPrintPdfCreate(String orderIds, String range,String batchNo, String supplierCode) {
        PurchaseOrderPrintDTO batchPrintData = purchaseOrderLogic.getBatchPrintData(orderIds, range, batchNo, supplierCode);
        return ResponseEntity.ok(purchaseOrderLogic.batchPrintPdfCreateNew(batchPrintData));
    }
    /**
     * 获取采购订单附件列表
     *
     * @param ids 采购订单ids 用半角逗号分割
     * @return
     */
    @GetMapping("attachmentList")
    public PageView attachmentList(String ids) {
        return purchaseOrderLogic.getAttachmentList(ids);
    }

    /**
     * 保存采购订单附件
     *
     * @param files 保存对象
     * @return
     */
    @PostMapping("saveAttachment")
    public ResponseEntity saveAttachment(@RequestBody List<OrderAttachmentDTO> files) {
        purchaseOrderLogic.saveAttachment(files);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除采购订单附件
     *
     * @param ids    采购订单附件的id 用半角逗号分割
     * @param remark 删除的备注
     * @return
     */
    @PostMapping("removeAttachment")
    public ResponseEntity removeAttachment(String ids,
                                           @RequestParam(required = false, defaultValue = "") String remark)
    {
        purchaseOrderLogic.removeAttachment(ids, remark);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改采购订单附件备注
     *
     * @param ids    采购订单附件的id 用半角逗号分割
     * @param remark 备注内容
     * @return
     */
    @PostMapping("submitRemark")
    public ResponseEntity submitRemark(String ids,
                                       @RequestParam(required = false, defaultValue = "") String remark)
    {
        purchaseOrderLogic.submitRemark(ids, remark);
        return ResponseEntity.ok().build();
    }

    /**
     * 供应商回复采购订单提交，需要供应商回复订单的数量.交期和备注
     *
     * @param replyDTOList 回复的订单明细数据
     * @return
     */
    @PostMapping("sumbitReply")
    public ResponseEntity sumbitReply(@RequestBody List<PurchaseOrderDetailReplyDTO> replyDTOList) {
        purchaseOrderLogic.sumbitReply(replyDTOList);
        return ResponseEntity.ok().build();
    }
    /**
     * 回签拒绝
     *
     * @param id 订单id
     * @param remark 回签拒绝原因
     * @return
     */
    @PostMapping("refusal")
    public ResponseEntity refusal(String remark,Long id) {
        purchaseOrderLogic.refusal(id,remark);
        return ResponseEntity.ok().build();
    }

    /**
     * 供应商交货计划保存
     * @param planBaseDTOS 交货计划数据
     * @return
     */
    @PostMapping("sumbitDevPlan")
    public ResponseEntity sumbitDevPlan(@RequestBody List<PurchaseOrderDevPlanBaseDTO> planBaseDTOS) {
        purchaseOrderLogic.sumbitDevPlan(planBaseDTOS);
        return ResponseEntity.ok().build();
    }

    /**
     * 财务确认采购订单
     *
     * @param ids 财务确认采购订单
     * @return
     */
    @PostMapping("updateFinConf")
    public ResponseEntity updateFinConf(String ids,
                                        @RequestParam(required = false, defaultValue = "0") Integer confirmFlag,
                                        @RequestParam(required = false) Integer attachmentType)
    {
        purchaseOrderLogic.updateFinConf(ids, confirmFlag,attachmentType);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过订单查找关联的对账单
     * @param id 订单id
     * @return 关联的对账单号列表
     */
    @GetMapping("/findAccountStatement")
    public List<String> findAccountStatementByOrderCode(Long id) {
        return purchaseOrderLogic.findAccountStatementByOrderCode(id);
    }
}
