package com.jwipc.supplier.controller.pc;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.page.PageView;
import com.jwipc.supplier.dto.SupplierMaterialDemoBaseDTO;
import com.jwipc.supplier.dto.SupplierMaterialDemoExamineDTO;
import com.jwipc.supplier.dto.SupplierMaterialDemoSaveDTO;
import com.jwipc.supplier.logic.SupplierMaterialDemoLogic;
import com.jwipc.supplier.model.query.MaterialDemoQuery;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

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

/**
 * <p>
 * 供应商样品申请 前端控制器
 * </p>
 *
 * @author JT
 * @since 2019-11-21
 */
@Slf4j
@RestController
@RequestMapping(MODULE_ROOT + "/materialDemo/rest")
@RequiredArgsConstructor
public class SupplierMaterialDemoRestController {
    private final SupplierMaterialDemoLogic supplierMaterialDemoLogic;

    /**
     * 供应商物料认证列表
     *
     * @param query
     * @return
     */
    @GetMapping("list")
    public PageView list(MaterialDemoQuery query) {
        return supplierMaterialDemoLogic.getList(query);
    }

    /**
     * 供应商物料认证列表
     *
     * @param query
     * @return
     */
    @GetMapping("listBySupplierId")
    public PageView listBySupplierId(MaterialDemoQuery query) {
        return supplierMaterialDemoLogic.listBySupplierId(query);
    }


    /**
     * 保存&更新
     *
     * @param saveDTO 样品主数据
     * @throws Exception
     */
    @PostMapping("save")
    public ResponseEntity save(@RequestBody SupplierMaterialDemoSaveDTO saveDTO) {
        supplierMaterialDemoLogic.saveOrUpdate(saveDTO);
        return ResponseEntity.ok().build();
    }


    /**
     * 供应商物料认证审核
     *
     * @param examineDto 审核对象
     * @return api
     */
    @PostMapping("examine")
    public ResponseEntity examine(@RequestBody SupplierMaterialDemoExamineDTO examineDto)
    {
        supplierMaterialDemoLogic.examine(examineDto);
        return ResponseEntity.ok().build();
    }

    @PostMapping("confirm")
    public ResponseEntity confirm(Long id) {
        supplierMaterialDemoLogic.confirm(id);
        return ResponseEntity.ok().build();
    }


    /**
     * 删除
     *
     * @param ids 要删除的ids
     * @return
     */
    @PostMapping("remove")
    public ResponseEntity remove(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        supplierMaterialDemoLogic.remove(idList);
        return ResponseEntity.ok().build();
    }

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

    /**
     * 供应商物料认证明细列表
     *
     * @param id
     * @return
     */
    @GetMapping("listByHeader")
    public PageView listByHeader(Long id) {
        return supplierMaterialDemoLogic.listByHeader(id);
    }
}
