package com.jwipc.order.controller.pc;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Pair;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.order.dto.*;
import com.jwipc.order.logic.DeliveryOrderLogic;
import com.jwipc.order.logic.DeliveryOrderPushMesLogic;
import com.jwipc.order.model.query.DeliveryOrderQuery;
import com.jwipc.page.PageView;
import com.jwipc.util.EasyExcelImportUtils;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

/**
 * <p> 送货单相关接口</p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping(MODULE_ROOT + "/deliveryOrder/rest")
@RequiredArgsConstructor
@Slf4j
public class DeliveryOrderRestController {
    private final DeliveryOrderLogic deliveryOrderLogic;

    private final DeliveryOrderPushMesLogic deliveryOrderPushMesLogic;

    /**
     * 送货单创建
     *
     * @param deliveryOrderSaveDTO
     * @return
     */
    @PostMapping("save")
    public ResponseEntity save(@RequestBody DeliveryOrderSaveDTO deliveryOrderSaveDTO) {
        final Pair<String, Set<String>> msg = deliveryOrderLogic.saveOrUpdate(deliveryOrderSaveDTO);
        JSONObject result = new JSONObject();
        result.put("id", msg.getKey());
        if (CollUtil.isNotEmpty(msg.getValue())) {
            result.put("msg", msg.getValue());
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 送货单修改
     *
     * @param deliveryOrderSaveDTO
     * @return
     */
    @PostMapping("update")
    public ResponseEntity update(@RequestBody DeliveryOrderSaveDTO deliveryOrderSaveDTO) {
        deliveryOrderLogic.update(deliveryOrderSaveDTO);
        return ResponseEntity.ok().build();
    }

    @AuditLogInter(value = "查询", module = "送货单")
    @GetMapping("list")
    public PageView list(DeliveryOrderQuery query) {
        return deliveryOrderLogic.getList(query);
    }

    /**
     * 送货单导出
     *
     * @return
     */
    @PostMapping("export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<DeliveryOrderBaseDTO> exportData = deliveryOrderLogic.getListAll(
                JSON.parseObject(queryStr, DeliveryOrderQuery.class));
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,
                "送货单数据-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN), "sheet", exportData,
                exportFiled);
    }

    /**
     * 获取送货单附件列表
     *
     * @param id 货单id
     * @return
     */
    @GetMapping("attachmentList")
    public PageView attachmentList(Long id) {
        return deliveryOrderLogic.getAttachmentList(id);
    }

    @PostMapping("saveAttachment")
    public ResponseEntity saveAttachment(@RequestBody List<DeliveryOrderFileDTO> fileDTOS) {
        deliveryOrderLogic.saveAttachment(fileDTOS);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除送货单附件
     *
     * @param ids    送货单id
     * @param remark 删除的备注
     * @return
     */
    @PostMapping("removeAttachment")
    public ResponseEntity removeAttachment(String ids,
                                           @RequestParam(required = false, defaultValue = "") String remark)
    {
        deliveryOrderLogic.removeAttachment(ids, remark);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/pushDeliveryToSap")
    public ResponseEntity pushDeliveryToSap(String ids) {
        deliveryOrderLogic.pushDeliveryToSap(ids);
        return ResponseEntity.ok(true);
    }

    @PostMapping("/updateReturnConfirmStatus")
    public ResponseEntity updateReturnConfirmStatus(String ids) {
        deliveryOrderLogic.updateReturnConfirmStatus(ids);
        return ResponseEntity.ok(true);
    }


    /**
     * 设置委托单号
     *
     * @param id 询价id
     */
    @PostMapping("/setEntrustNo")
    public ResponseEntity setEntrustNo(Long id,String entrustNo) {
        deliveryOrderLogic.setEntrustNo(id,entrustNo);
        return ResponseEntity.ok(true);
    }

    /**
     * 修改送货单附件备注
     *
     * @param ids    送货单id
     * @param remark 备注内容
     * @return
     */
    @PostMapping("submitRemark")
    public ResponseEntity submitRemark(String ids,
                                       @RequestParam(required = false, defaultValue = "") String remark)
    {
        deliveryOrderLogic.submitRemark(ids, remark);
        return ResponseEntity.ok().build();
    }

    @GetMapping("print")
    public ResponseEntity print(String ids) {
        final DeliveryPrintDTO printData = deliveryOrderLogic.getPrintData(ids);
        return ResponseEntity.ok(printData);
    }

    @PostMapping("import")
    public ResponseEntity upload(MultipartFile file) throws IOException {
        List<Map<String, String>> dataList = EasyExcelImportUtils.parseExcelToData(file.getInputStream(), 2);
        //
        List<ImportCreateDeliveryOrderDetailDTO> result = deliveryOrderLogic.getImportData(dataList);
        return ResponseEntity.ok(result);
    }

    @PostMapping("overseasImport")
    public ResponseEntity overseasImport(MultipartFile file,String factoryCode) throws IOException {
        List<ImportCreateDeliveryOrderDetailDTO> result = deliveryOrderLogic.overseasImport(file,factoryCode);
        return ResponseEntity.ok(result);
    }

   //送货单数据重推
   @PostMapping("/re/deliveryNote")
   public ResponseEntity reDeliveryNote(String id) {
       deliveryOrderPushMesLogic.reDeliveryNote(id);
       return ResponseEntity.ok(true);
   }

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

    @GetMapping("/getDevOrderCodesByEntrustNo")
    public ResponseEntity getDevOrderCodesByEntrustNo(String entrustNo) {
        return ResponseEntity.ok(deliveryOrderLogic.getDevOrderCodesByEntrustNo(entrustNo));
    }

    @RequestMapping("/getFtp")
    public ResponseEntity getFtp(String startDate , String endDate) {
        deliveryOrderLogic.getFtp(startDate,endDate);

        return ResponseEntity.ok(true);
    }
}
