package com.gree.mall.manager.controller.common;


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beust.jcommander.Parameter;
import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.common.SysDictCompanyVO;
import com.gree.mall.manager.bean.common.SysDictTypeVO;
import com.gree.mall.manager.bean.common.SysDictVO;
import com.gree.mall.manager.bean.websit.WebsitPartsRetVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.common.SysDictCompanyLogic;
import com.gree.mall.manager.logic.common.SysDictLogic;
import com.gree.mall.manager.plus.entity.SysDict;
import com.gree.mall.manager.plus.entity.SysDictCompany;
import com.gree.mall.manager.plus.entity.SysDictType;
import com.gree.mall.manager.plus.entity.SysDictTypeCompany;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;


@Validated
@RequiredArgsConstructor
@RestController
@Api(value = "辅材字典业务配置", tags ={"辅材字典业务配置"} )
@RequestMapping("dictCompany")
public class SysDictCompanyController {

    private final SysDictCompanyLogic sysDictCompanyLogic;

    @ZfireList
    @PostMapping("page")
    @ApiOperation(value ="字典-分页")
    public ResponseHelper<IPage<SysDictCompanyVO>> sysDictList(@RequestBody ZfireParamBean zfireParamBean) {
        IPage<SysDictCompanyVO> sysDictIPage = sysDictCompanyLogic.sysDictIPage(zfireParamBean);
        return ResponseHelper.success(sysDictIPage, new TypeReference<SysDictCompanyVO>() {
        });
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "字典-分页导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<SysDictCompanyVO> sysDictIPage = sysDictCompanyLogic.sysDictIPage(zfireParamBean);
        //3.导出
        FieldUtils.exportData(sysDictIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @ApiNotAuth
    @PostMapping("listDict")
    @ApiOperation(value = "下拉框查询")
    public ResponseHelper<List<SysDictCompany>> sysDictBeanList(
            @ApiParam(value = "类型", required = true) @RequestParam(required = true) String dictType,
            @ApiParam(value = "父字典类型", required = false) @RequestParam(required = false) String parentDictType,
            @ApiParam(value ="父code", required = false) @RequestParam(required = false) String parentDictCode,
            @ApiParam(value = "家用/商用", required = false) @RequestParam(required = false) String flag,
            @ApiParam(value = "服务单来源", required = false) @RequestParam(required = false) String source
    ) {

        List<SysDictCompany> sysDicts = sysDictCompanyLogic.getSysDictList(dictType);
        return ResponseHelper.success(sysDicts);
    }
    @ApiNotAuth
    @PostMapping("type/list")
    @ApiOperation(value = "字典类型")
    public ResponseHelper<List<SysDictTypeCompany>> typeListAll() {
        return ResponseHelper.success(sysDictCompanyLogic.typeList(null));
    }



    @PostMapping("addType")
    @ApiOperation(value = "字典类型-添加")
    public ResponseHelper addType(@Valid @RequestBody SysDictType param) {
        sysDictCompanyLogic.addType(param);
        return ResponseHelper.success();
    }

    @PostMapping("updateType")
    @ApiOperation(value = "字典类型-修改")
    public ResponseHelper updateType(@Valid @RequestBody SysDictType param) {
        sysDictCompanyLogic.updateType(param);
        return ResponseHelper.success();
    }

    @PostMapping("delType")
    @ApiOperation(value = "字典类型-删除")
    public ResponseHelper delType(
            @ApiParam(value  = "字典值id")
            @NotBlank(message = "字典值id不能为空")
            @RequestParam String id) {
        sysDictCompanyLogic.delType(id, true);
        return ResponseHelper.success();
    }

    @PostMapping("detailType")
    @ApiOperation(value = "字典类型-详情")
    public ResponseHelper<SysDictTypeCompany> detailType(
            @ApiParam(value = "字典值id")
            @NotBlank(message = "字典值id不能为空")
            @RequestParam String id) {
        SysDictTypeCompany sysDict = sysDictCompanyLogic.detailType(id);
        return ResponseHelper.success(sysDict);
    }

    @PostMapping("typeList")
     @ApiOperation(value = "字典类型")
    public ResponseHelper<List<SysDictTypeVO>> typeList() {
        return ResponseHelper.success(sysDictCompanyLogic.typeList(true));
    }

/*    @PostMapping("listDict")
     @ApiOperation(value = "字典值列表")
    public ResponseHelper<List<SysDict>> dictList(
            @ApiOperation(value = "字典类型", required = true)
            @NotBlank(message = "字典类型不能为空")

            @RequestParam String type) {
        List<SysDict> dictVOList = sysDictLogic.dictByType(type, true);
        return ResponseHelper.success(dictVOList);
    }*/

    @PostMapping("add")
     @ApiOperation(value = "字典-添加")
    public ResponseHelper add(@Valid @RequestBody SysDictCompany param) {
        sysDictCompanyLogic.saveOpenDict(param);
        return ResponseHelper.success();
    }

    @PostMapping("update")
     @ApiOperation(value = "字典-修改")
    public ResponseHelper update(@Valid @RequestBody SysDictCompany param) {
        sysDictCompanyLogic.updateOpenDict(param);
        return ResponseHelper.success();
    }

    @PostMapping("del")
     @ApiOperation(value = "字典-删除")
    public ResponseHelper del(
            @ApiParam(value  = "字典值id")
            @NotBlank(message = "字典值id不能为空")
            @RequestParam String id) {
        sysDictCompanyLogic.delete(id, true);
        return ResponseHelper.success();
    }

    @PostMapping("detail")
     @ApiOperation(value = "字典-详情")
    public ResponseHelper<SysDictCompany> detail(
            @ApiParam(value = "字典值id")
            @NotBlank(message = "字典值id不能为空")
            @RequestParam String id) {
        SysDictCompany sysDict = sysDictCompanyLogic.detail(id);
        return ResponseHelper.success(sysDict);
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "导入")
    public ResponseHelper importExcel(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
            HttpServletRequest request
    ) throws RemoteServiceException, IOException, ParseException {
        List<Object> objects = ExcelUtils.importExcel(file);
        sysDictCompanyLogic.importExcel(objects,request);
        return ResponseHelper.success();
    }

}
