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.MoldFormDTO;
import com.jwipc.sys.dto.MoldMoveDTO;
import com.jwipc.sys.logic.MoldFormLogic;
import com.jwipc.sys.model.param.MoldFormParam;
import com.jwipc.sys.model.query.MoldFormQuery;
import com.jwipc.sys.model.query.MoldMoveQuery;
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-01-31 10:53
 * @desc 模具
 */

@RestController
@RequestMapping(MODULE_ROOT + "/mold/moldForm/rest/")
@RequiredArgsConstructor
public class MoldFormRestController {
    private final MoldFormLogic moldFormLogic;
    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(MoldFormQuery query) {
        return moldFormLogic.findByPage(query);
    }

    /**
     * 模具列表导出
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @AuditLogInter(value = "导出", module = "模具列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<MoldFormDTO> exportData = moldFormLogic.listAll1(JSON.parseObject(queryStr, MoldFormQuery.class));
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"模具列表信息导出-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }

    /**
     * 移模列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/moldMove/list")
    public PageView moldMoveList(MoldMoveQuery query) {
        return moldFormLogic.moldMoveFindByPage(query);
    }
//    @RequestMapping("/moldMove/list1")
//    public List<MoldDTO>  moldMoveList1(Long moldId) {
//        return moldFormLogic.moldMoveFindByPage1(moldId);
//    }
    /**
     * 移模保存更新
     * @param param 参数
     */
    @PostMapping("/moldMove/saveOrUpdate")
    public ResponseEntity moldMoveSaveOrUpdate(MoldMoveDTO param){
        moldFormLogic.moldMoveSaveOrUpdate(param);
        return ResponseEntity.ok(true);
    }
    /**
     * 移模删除
     *
     * @param id 参数
     */
    @PostMapping("/moldMove/remove")
    public ResponseEntity moldMoveRemove(MoldMoveDTO param) {
        moldFormLogic.moldMoveRemove(param);
        return ResponseEntity.ok(true);
    }

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

    /**
     * 保存更新
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(MoldFormParam param){
        moldFormLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }
    /**
     * 删除
     *
     * @param moldCode 参数
     */
    @PostMapping("/remove")
    public ResponseEntity remove(String moldCode) {
        moldFormLogic.remove(moldCode);
        return ResponseEntity.ok(true);
    }
    /**
     * 公司主数据
     * @return 信息
     */
    @RequestMapping("/listAll")
    public List<MoldFormDTO> listAll(MoldFormQuery query){
        return moldFormLogic.listAll(query);
    }
}
