package com.jwipc.order.controller.pc;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.order.dto.DeliveryOrderDetailListDTO;
import com.jwipc.order.logic.DeliveryOrderDetailLogic;
import com.jwipc.order.model.query.DeliveryOrderDetailQuery;
import com.jwipc.page.PageView;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

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

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

    /**
     * 所有送货单明细，如果是供应商查询供应商所有送单明细
     *
     * @param query 查询参数
     * @return
     */
    @AuditLogInter(value = "查询", module = "送货单明细查看")
    @GetMapping("list")
    public PageView list(DeliveryOrderDetailQuery query) {
        return deliveryOrderDetailLogic.getList(query);
    }


    /**
     * 送货单明细数据
     *
     * @param id 送货单主键
     * @return
     */
    @GetMapping("listForDeliverOrder")
    public PageView listForDeliverOrder(Long id) {
        return deliveryOrderDetailLogic.getListForDeliverOrder(id);
    }

    /**
     * 采购订单明细导出
     *
     * @return
     */
    @PostMapping("export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<DeliveryOrderDetailListDTO> exportData = deliveryOrderDetailLogic.getListAll(
                JSON.parseObject(queryStr, DeliveryOrderDetailQuery.class));


        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,
                "送货单明细数据-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN), "sheet", exportData,
                exportFiled);
    }

    @PostMapping("remove")
    public ResponseEntity remove(String ids) {
        deliveryOrderDetailLogic.remove(ids);
        return ResponseEntity.ok().build();
    }

    @PostMapping("updateClose")
    public ResponseEntity updateClose(String ids) {
        deliveryOrderDetailLogic.updateClose(ids);
        return ResponseEntity.ok().build();

    }
}
