package com.mortals.xhx.module.demo.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.AjaxResult;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.utils.poi.ExcelUtil;
import com.mortals.xhx.base.system.param.service.ParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.code.YesNo;
import com.mortals.xhx.module.demo.model.DemoEntity;
import com.mortals.xhx.module.demo.service.DemoService;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DemoController
 * <p>
 * 测试demo 控制器
 *
 * @author zxfei
 * @date 2021-10-09
 */
@RestController
@RequestMapping("demo")
public class DemoController extends BaseCRUDJsonMappingController<DemoService, DemoForm, DemoEntity, String> {

    @Autowired
    private ParamService paramService;

    public DemoController() {
        super.setFormClass(DemoForm.class);
        super.setModuleDesc("测试demo");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, DemoForm form, Map<String, Object> model, Context context) {
        //this.addDict(model, "fieldThree", paramService.getParamByFirstOrganize("fieldThree"));
        super.init(request, response, form, model, context);
    }

//    @PostMapping("exportExcel")
//    public void exportExcel(DemoForm form) {
//        try {
//            PageInfo pageInfo = new PageInfo(65535);
//            List<DemoEntity> list = this.service.find(form.getQuery(), pageInfo, getContext()).getList();
//            ExcelUtil<DemoEntity> util = new ExcelUtil<>(DemoEntity.class);
//            byte[] data = util.exportExcel(list, "demo");
//            this.responseStream(response, data, "demo.xlsx");
//        } catch (Exception e) {
//            this.doException(request, "导出异常", new HashMap<String, Object>(), e);
//        }
//    }

//    @PostMapping("/downloadTemplate")
//    public void downloadTemplate() {
//        try {
//            ExcelUtil<DemoEntity> util = new ExcelUtil<>(DemoEntity.class);
//            byte[] data = util.importTemplateExcel("用户数据");
//            this.responseStream(response, data, "demo_template.xlsx");
//        } catch (Exception e) {
//            this.doException(request, "导出模板异常", new HashMap<String, Object>(), e);
//        }
//    }

//    @PostMapping("/importData")
//    public String importData(MultipartFile file, boolean updateSupport) {
//        JSONObject ret = new JSONObject();
//        String busiDesc = "导入数据";
//        int code = VALUE_RESULT_SUCCESS;
//        try {
//            ExcelUtil<DemoEntity> util = new ExcelUtil<>(DemoEntity.class);
//            List<DemoEntity> demoList = util.importExcel(file.getInputStream());
//            String message = this.service.importDemoList(demoList, updateSupport, getContext());
//            ret.put(KEY_RESULT_MSG, message);
//            recordSysLog(request, busiDesc + " 【成功】");
//        } catch (Exception e) {
//            code = VALUE_RESULT_FAILURE;
//        }
//        ret.put(KEY_RESULT_CODE, code);
//        return ret.toJSONString();
//    }

}