package com.jwipc.sys.controller.pc;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.jwipc.page.PageView;
import com.jwipc.sys.dto.DictDTO;
import com.jwipc.sys.dto.DictEditDTO;
import com.jwipc.sys.dto.DictItemEditDTO;
import com.jwipc.sys.dto.DictItemListDTO;
import com.jwipc.sys.logic.DictLogic;
import com.jwipc.sys.model.query.DictListQuery;
import com.jwipc.sys.model.query.DictTypeQuery;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

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

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping(MODULE_ROOT + "/dict")
@RequiredArgsConstructor
public class DictRestController {
    private final DictLogic dictLogic;

    @GetMapping("getDicByType")
    public ResponseEntity getDicByType(@RequestParam(value = "type", required = true) String type) {
        List<DictDTO> result = dictLogic.getDicByType(type);
        return ResponseEntity.ok(result);
    }

    @GetMapping("getDicByTypes")
    public ResponseEntity getDicByTypes(@RequestParam(value = "types", required = true) String types) {
        Map<String, List<DictDTO>> result = dictLogic.getDicByTypes(types);
        return ResponseEntity.ok(result);
    }

    @GetMapping("list")
    public PageView list(DictListQuery query) {
        return dictLogic.list(query);
    }

    @GetMapping("itemList")
    public PageView itemList(DictTypeQuery query) {
        return dictLogic.itemList(query);
    }

    @PostMapping("saveOrUpdate")
    public ResponseEntity saveOrUpdate(DictEditDTO dictEditDTO) {
        dictLogic.saveOrUpdate(dictEditDTO);
        return ResponseEntity.ok().build();
    }
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr,Long dictType) throws IOException {
        DictTypeQuery query = new DictTypeQuery();
        query.setDictType(dictType);
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<DictItemListDTO> exportData = dictLogic.listAll(query);
//        final List<DictItemListDTO> exportData = dictLogic.listAll(JSON.parseObject(queryStr, DictTypeQuery.class));
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"字典信息导出-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }
    @PostMapping("item/saveOrUpdate")
    public ResponseEntity itemSaveOrUpdate(DictItemEditDTO dictEditDTO) {
        dictLogic.itemSaveOrUpdate(dictEditDTO);
        return ResponseEntity.ok().build();
    }

    @PostMapping("item/remove")
    public ResponseEntity itemRemove(Long id) {
        dictLogic.itemRemove(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("item/importData")
    public ResponseEntity importData(@RequestParam(value = "file") MultipartFile file, Long dictTypeId) {
        dictLogic.itemImportData(file, dictTypeId);
        return ResponseEntity.ok().build();
    }
}
