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.entity.SapGoodReceipt;
import com.jwipc.order.logic.GoodReceiptLogic;
import com.jwipc.order.model.query.SapGoodReceiptQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 java.util.Objects;

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

/**
 * @Author: swq
 * @Date: 2024/4/8 9:48
 */
@RestController
@RequestMapping(MODULE_ROOT + "/goodReceipt/rest")
@RequiredArgsConstructor
@Slf4j
public class GoodReceiptRestController {
    private final GoodReceiptLogic goodReceiptLogic;
    /**
     * 绩效考核报表查询
     *
     * @param query 查询参数
     * @return
     */
    @AuditLogInter(value = "查询", module = "退货凭证查询")
    @GetMapping("/list")
    public PageView list(SapGoodReceiptQuery query) {
        if(StringUtils.isEmpty(query.getWerks())){
            query.setWerks("1000");
        }
        if(LoginUtil.isSupplier()){
            query.setLifnr(LoginUtil.getAccount());
        }else{
            query.setUserId(LoginUtil.getUserId());
        }
        return goodReceiptLogic.goodReceiptList(query);
    }

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

}
