package cn.ey.biz.module.system.controller.admin.category;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.common.util.i18n.I18nContextHolder;
import cn.ey.biz.framework.excel.core.util.ExcelUtils;
import cn.ey.biz.framework.operatelog.core.annotations.OperateLog;
import cn.ey.biz.module.system.controller.admin.category.vo.*;
import cn.ey.biz.module.system.convert.category.CategoryDataConvert;
import cn.ey.biz.module.system.dal.dataobject.app.AppDO;
import cn.ey.biz.module.system.dal.dataobject.category.CategoryDataDO;
import cn.ey.biz.module.system.dal.mapper.app.AppMapper;
import cn.ey.biz.module.system.service.category.CategoryDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.ACCEPT_LANGUAGE_NOT_NULL;

@Api(tags = "管理后台 - 字典分类数据")
@RestController
@RequestMapping("/system/category-data")
@Validated
public class CategoryDataController {

    @Resource
    private CategoryDataService categoryDataService;

    @Resource
    private AppMapper appMapper;

    @PostMapping("/create")
    @ApiOperation("创建字典分类数据")
    @PreAuthorize("@ss.hasPermission('system:category-data:create')")
    public R<Long> createCategoryData(@Valid @RequestBody CategoryDataCreateReqVO createReqVO) {
        return success(categoryDataService.createCategoryData(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新字典分类数据")
    @PreAuthorize("@ss.hasPermission('system:category-data:update')")
    public R<Boolean> updateCategoryData(@Valid @RequestBody CategoryDataUpdateReqVO updateReqVO) {
        categoryDataService.updateCategoryData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除字典分类数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:category-data:delete')")
    public R<Boolean> deleteCategoryData(@RequestParam("id") Long id) {
        categoryDataService.deleteCategoryData(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得字典分类数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:category-data:query')")
    public R<CategoryDataRespVO> getCategoryData(@RequestParam("id") Long id) {
        CategoryDataDO categoryData = categoryDataService.getCategoryData(id);
        return success(CategoryDataConvert.INSTANCE.convert(categoryData));
    }

    @GetMapping("/list-category-bypid")
    @ApiOperation("获得字典父Id所有分类数据")
    @ApiImplicitParam(name = "pid", value = "编号", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:category-data:query')")
    public R<List<CategoryDataDO>> getCategoryByidData(@RequestParam("pid") Long pid) {
        List<CategoryDataDO> categoryData = categoryDataService.getCategoryByidData(pid);
        return success(categoryData);
    }

    @GetMapping("/list")
    @ApiOperation("获得字典分类数据列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:category-data:query')")
    public R<List<CategoryDataRespVO>> getCategoryDataList(@RequestParam("ids") Collection<Long> ids) {
        List<CategoryDataDO> list = categoryDataService.getCategoryDataList(ids);
        return success(CategoryDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获得全部字典分类数据列表", notes = "一般用于管理后台缓存字典数据在本地")
    // 无需添加权限认证，因为前端全局都需要
    public R<List<CategoryDataSimpleRespVO>> getSimpleCategoryDatas(@RequestParam("app") String app) {
        String acceptLanguage = I18nContextHolder.getLangId();
        if(StringUtils.isEmpty(acceptLanguage)){
            throw exception(ACCEPT_LANGUAGE_NOT_NULL);
        }
        List<CategoryDataSimpleRespVO> list = categoryDataService.getCategoryDatas(app,acceptLanguage);
        return success(CategoryDataConvert.INSTANCE.convertLists(list));
    }

    @GetMapping("/list-all-simple-with-app")
    @ApiOperation(value = "获得全部字典分类数据列表", notes = "一般用于管理后台缓存字典数据在本地")
    @ApiImplicitParam(name = "app", value = "应用", dataTypeClass = String.class)
    public R<List<CategoryDataSimpleRespVO>> getSimpleCategoryWithAppData(@RequestParam("app") String app) {
        String acceptLanguage = I18nContextHolder.getLangId();
        if(StringUtils.isEmpty(acceptLanguage)){
            throw exception(ACCEPT_LANGUAGE_NOT_NULL);
        }
        List<CategoryDataSimpleRespVO> list = categoryDataService.getCategoryDatas(app,acceptLanguage);
        List<AppDO> appDOS = appMapper.selectList();
        for (AppDO appDO : appDOS) {
                CategoryDataSimpleRespVO respVO = new CategoryDataSimpleRespVO();
                respVO.setLabel(appDO.getName());
                respVO.setDictType("app"); // app
            respVO.setPid(0L);
            respVO.setValue(appDO.getCode());
                list.add(respVO);
        }
        return success(list);
    }

    @GetMapping("/page")
    @ApiOperation("获得字典分类数据分页")
    @PreAuthorize("@ss.hasPermission('system:category-data:query')")
    public R<PageResult<CategoryDataRespVO>> getCategoryDataPage(@Valid CategoryDataPageReqVO pageVO) {
        String acceptLanguage = I18nContextHolder.getLangId();
        if(StringUtils.isEmpty(acceptLanguage)){
            throw exception(ACCEPT_LANGUAGE_NOT_NULL);
        }
        pageVO.setAcceptLanguage(acceptLanguage);
        PageResult<CategoryDataRespVO> pageResult = categoryDataService.getCategoryDataPage(pageVO);

        return success(CategoryDataConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出字典分类数据 Excel")
    @PreAuthorize("@ss.hasPermission('system:category-data:export')")
    @OperateLog(type = EXPORT)
    public void exportCategoryDataExcel(@Valid CategoryDataExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CategoryDataDO> list = categoryDataService.getCategoryDataList(exportReqVO);
        // 导出 Excel
        List<CategoryDataExcelVO> datas = CategoryDataConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "字典分类数据.xls", "数据", CategoryDataExcelVO.class, datas);
    }

    @GetMapping("/get-category-data-byid")
    @ApiOperation("根据ID获取字典多语言信息")
    @PreAuthorize("@ss.hasPermission('system:category-data:query')")
    public R<CategoryDataRespVO> getCategoryDataByid(Long id) {
        CategoryDataRespVO categoryDataRespVO = categoryDataService.getCategoryDataByid(id);
        return success(categoryDataRespVO);
    }

}
