package com.jwipc.sys.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.page.PageView;
import com.jwipc.sys.dto.MoldSettingsDTO;
import com.jwipc.sys.logic.MoldSettingsLogic;
import com.jwipc.sys.model.param.MoldSettingsParam;
import com.jwipc.sys.model.query.MoldSettingsQuery;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * @author Al
 * @create 2024-02-01 10:44
 */
@RestController
@RequestMapping(MODULE_ROOT + "/mold/moldSettings/rest/")
@RequiredArgsConstructor
public class MoldSettingsRestController {
    private final MoldSettingsLogic moldSettingsLogic;
    /**
     * 列表
     *
     * @param query
     * @return
     */
    @AuditLogInter(value = "首页列表数据", module = "摸具与产品关联")
    @RequestMapping("/list")
    public PageView list(MoldSettingsQuery query) {
        return moldSettingsLogic.findByPage(query);
    }
    /**
     * 保存更新
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(MoldSettingsParam param){
        moldSettingsLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }
    /**
     * 产品导出
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @AuditLogInter(value = "导出数据", module = "摸具与产品关联")
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<MoldSettingsDTO> exportData = moldSettingsLogic.listAll(JSON.parseObject(queryStr, MoldSettingsQuery.class));
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"模具产品关联信息导出-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }
    /**
     * 删除
     *
     * @param productCode 参数
     */
    @PostMapping("/remove")
    public ResponseEntity remove(String productCode) {
        moldSettingsLogic.remove(productCode);
        return ResponseEntity.ok(true);
    }

    /**
     * 模具产品关联保存附件
     *
     * @param id      品类id
     * @param fileIds 附件id 逗号分割
     * @return
     */
    @PostMapping("saveOnFile")
    public ResponseEntity saveOnFile(Long id, String fileIds) {
        moldSettingsLogic.saveOnFile(id, fileIds);
        return ResponseEntity.ok().build();
    }
}
