package com.dayou.controller;

import com.dayou.vo.IdNameVO;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IDictDataService;
import com.dayou.entity.DictData;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;
import com.dayou.utils.HttpKit;
import com.dayou.exception.ErrorCode;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.multipart.MultipartFile;
/**
 * 字典数据
 *
 * @author wucl
 * @since 2022-11-29
 */
@RestController
@RequestMapping("dictData")
@Slf4j
public class DictDataController extends BaseController {
    @Autowired
    private IDictDataService dictDataService;

    /**
    * 字典数据列表
    */
    @GetMapping("")
    public RestResponse<Page<DictData>> page(DictData dictData, Page page){
        Page<DictData> pages=dictDataService.selectPage(page,dictData);
        return RestResponse.data(pages);
    }

    /**
     * 字典数据详情
     */
    @GetMapping("/{id}")
    public RestResponse<DictData> detail(@PathVariable Long id){
        DictData xDictData =dictDataService.detail(id);
        return RestResponse.data(xDictData);
     }

    /**
     * 字典数据新增
     */
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody DictData dictData) {
        Boolean ret = dictDataService.add(dictData);
        return RestResponse.data(ret);
    }

    /**
     * 字典数据更新
     */
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody DictData dictData) {
        Boolean ret = dictDataService.update(dictData);
        return RestResponse.data(ret);
    }

    /**
     * 字典数据删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = dictDataService.delete(id);
        return RestResponse.data(ret);
    }


    /**
     * 字典数据下拉列表
     */
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(){
        LambdaQueryWrapper<DictData> lambdaQueryWrapper = new LambdaQueryWrapper<DictData>()
        .select(DictData::getId,DictData::getName)
        .eq(DictData::getDeleted,false);
        List<DictData> list= dictDataService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }

    /**
     * 字典数据下拉列表
     */
    @GetMapping("/simple/{type}")
    public RestResponse<List<PullDownModel>> simpleType(@PathVariable("type") String type){
        List<PullDownModel> result = dictDataService.simpleType(type);
        return RestResponse.data(result);
    }

    /**
     * 字典数据含配置值（remark）
     */
    @GetMapping("/remark/{code}")
    public RestResponse<List<DictData>> byCode(@PathVariable("code") String code){
        List<DictData> result = dictDataService.byCode(code);
        return RestResponse.data(result);
    }
    /**
     * 字典数据模板下载
     */
    @GetMapping(value = "/download/importTemplate")
    public void importTemplate() {
        try {
            XSSFWorkbook wb = dictDataService.generateImportTemplate();

            String fileName = "字典数据导入模板" + System.currentTimeMillis() + ".xlsx";

            writeXss2Response(HttpKit.getRequest(), HttpKit.getResponse(), wb, fileName);
        } catch (Exception e) {
            log.error("生成字典数据导入模板异常", e);
            ErrorCode.throwBusinessException(ErrorCode.DEFAULTERROR);
        }
    }

    /**
     * 字典数据导入
     */
    @PostMapping("/importExcel")
    public RestResponse<Boolean> importExcel(@RequestPart("file") MultipartFile file) {
        dictDataService.importExcel(file);
        return RestResponse.data(true);
    }

    /**
     * 字典数据导出
     */
    @GetMapping(value = "/download/exportExcel")
    public void export(DictData dictData) {
        try {
            XSSFWorkbook wb =  dictDataService.exportExcel(dictData);

            String fileName = "字典数据" + System.currentTimeMillis() + ".xlsx";

            writeXss2Response(HttpKit.getRequest(), HttpKit.getResponse(), wb, fileName);
        } catch (Exception e) {
            log.error("导出字典数据", e);
            ErrorCode.throwBusinessException(ErrorCode.DEFAULTERROR);
        }

    }



}

