package com.zt.plat.module.qms.common.dic.controller.admin;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.common.dic.controller.vo.DictionaryBusinessPageReqVO;
import com.zt.plat.module.qms.common.dic.controller.vo.DictionaryBusinessRespVO;
import com.zt.plat.module.qms.common.dic.controller.vo.DictionaryBusinessSaveReqVO;
import com.zt.plat.module.qms.common.dic.dal.dataobject.DictionaryBusinessDO;
import com.zt.plat.module.qms.common.dic.service.DictionaryBusinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 业务参数字典")
@RestController
@RequestMapping("/qms/dictionary-business")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class DictionaryBusinessController implements BusinessControllerMarker {


    @Resource
    private DictionaryBusinessService dictionaryBusinessService;


    @GetMapping("/getDataListByCategoryKey")
    @Operation(summary = "根据分类Key查询数据列表")
    public CommonResult<List<DictionaryBusinessRespVO>> getDataListByCategoryKey(@RequestParam(required = true) String key) {
//        String key = pageReqVO.getKey();
//        if(ObjectUtils.isEmpty(key))
//            return CommonResult.error(MISS_PARAMETER);
        List<DictionaryBusinessDO> list = dictionaryBusinessService.queryDictItemsByKey(key);
        return success(BeanUtils.toBean(list, DictionaryBusinessRespVO.class));
    }

    @GetMapping("/getDataByDataKey")
    @Operation(summary = "根据数据Key查询数据")
    public CommonResult<DictionaryBusinessRespVO> getDataByDataKey(@RequestParam(required = true) String key) {
//        String key = pageReqVO.getKey();
        return dictionaryBusinessService.getDataByDataKey(key);
    }

    @PostMapping("/saveData")
    @Operation(summary = "保存数据")
    public CommonResult<DictionaryBusinessRespVO> saveData(@Valid @RequestBody DictionaryBusinessSaveReqVO createReqVO) {
        return dictionaryBusinessService.saveData(createReqVO);
    }

    @PostMapping("/saveClassify")
    @Operation(summary = "保存分类")
    public CommonResult<DictionaryBusinessRespVO> saveClassify(@Valid @RequestBody DictionaryBusinessSaveReqVO createReqVO) {
        return dictionaryBusinessService.saveCategory(createReqVO);
    }

    @GetMapping("/getTreeData")
    @Operation(summary = "查询分类树")
    public CommonResult<List<DictionaryBusinessRespVO>> getTreeData() {
        List<DictionaryBusinessDO> list = dictionaryBusinessService.getTreeData();
        return success(BeanUtils.toBean(list, DictionaryBusinessRespVO.class));
    }

//    @PostMapping("/create")
//    @Operation(summary = "创建业务参数字典")
//    @PreAuthorize("@ss.hasPermission('qms:dictionary-business:create')")
//    public CommonResult<DictionaryBusinessRespVO> createDictionaryBusiness(@Valid @RequestBody DictionaryBusinessSaveReqVO createReqVO) {
//        return success(dictionaryBusinessService.createDictionaryBusiness(createReqVO));
//    }
//
//    @PutMapping("/update")
//    @Operation(summary = "更新业务参数字典")
//    @PreAuthorize("@ss.hasPermission('qms:dictionary-business:update')")
//    public CommonResult<Boolean> updateDictionaryBusiness(@Valid @RequestBody DictionaryBusinessSaveReqVO updateReqVO) {
//        dictionaryBusinessService.updateDictionaryBusiness(updateReqVO);
//        return success(true);
//    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务参数字典")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:dictionary-business:delete')")
    public CommonResult<Boolean> deleteDictionaryBusiness(@RequestParam("id") Long id) {
        dictionaryBusinessService.deleteDictionaryBusiness(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除业务参数字典")
    @PreAuthorize("@ss.hasPermission('qms:dictionary-business:delete')")
    public CommonResult<Boolean> deleteDictionaryBusinessList(@RequestBody BatchDeleteReqVO req) {
        dictionaryBusinessService.deleteDictionaryBusinessListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务参数字典")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:dictionary-business:query')")
    public CommonResult<DictionaryBusinessRespVO> getDictionaryBusiness(@RequestParam("id") Long id) {
        DictionaryBusinessDO dictionaryBusiness = dictionaryBusinessService.getDictionaryBusiness(id);
        return success(BeanUtils.toBean(dictionaryBusiness, DictionaryBusinessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务参数字典分页")
//    @PreAuthorize("@ss.hasPermission('qms:dictionary-business:query')")
    public CommonResult<PageResult<DictionaryBusinessRespVO>> getDictionaryBusinessPage(@Valid DictionaryBusinessPageReqVO pageReqVO) {
        PageResult<DictionaryBusinessDO> pageResult = dictionaryBusinessService.getDictionaryBusinessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictionaryBusinessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务参数字典 Excel")
    @PreAuthorize("@ss.hasPermission('qms:dictionary-business:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictionaryBusinessExcel(@Valid DictionaryBusinessPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictionaryBusinessDO> list = dictionaryBusinessService.getDictionaryBusinessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "业务参数字典.xls", "数据", DictionaryBusinessRespVO.class,
                        BeanUtils.toBean(list, DictionaryBusinessRespVO.class));
    }

}