package com.bc.exam.modules.dict.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bc.exam.core.api.ApiRest;
import com.bc.exam.core.api.controller.BaseController;
import com.bc.exam.core.api.dto.BaseIdsReqDTO;
import com.bc.exam.core.api.dto.BaseStateReqDTO;
import com.bc.exam.core.api.dto.PagingReqDTO;
import com.bc.exam.core.exception.ServiceException;
import com.bc.exam.modules.dict.entity.SysDictData;
import com.bc.exam.modules.dict.entity.SysDictType;
import com.bc.exam.modules.dict.service.SysDictDataService;
import com.bc.exam.modules.dict.service.SysDictTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Description 描述:字典 码值大类表 前端控制器
 * @Author A贾宇婷034244310
 * @Date 20260615
 */
@RestController
@RequestMapping("/sys/api/dict/type")
public class SysDictTypeController extends BaseController {
    @Resource
    private SysDictTypeService baseService;

    @Resource
    private SysDictDataService dataService;

    /**
     * 分页查找
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public ApiRest<IPage<SysDictType>> paging(@RequestBody PagingReqDTO<SysDictType> reqDTO) {
        //分页查询并转换
        IPage<SysDictType> page = baseService.paging(reqDTO);
        return super.success(page);
    }

    @ApiOperation(value = "修改状态")
    @PostMapping("/state")
    public ApiRest state(@RequestBody BaseStateReqDTO reqDTO) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(SysDictType::getId, reqDTO.getIds());
        SysDictType record = new SysDictType();
        record.setState(reqDTO.getState());
        baseService.update(record, wrapper);
        return super.success();
    }

    /**
     * 批量删除
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/delete")
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        List<SysDictType> sysDictTypes = baseService.listByIds(reqDTO.getIds());
        List<String> codes = sysDictTypes.stream()
                .map(SysDictType::getCode)
                .collect(Collectors.toList());
        List<SysDictData> datas = dataService.list(new QueryWrapper<SysDictData>().in("CODE", codes));
        if (CollectionUtil.isNotEmpty(datas)) {
            throw new ServiceException("请先删除对应的码值后在删除码值大类!");
        }
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
     * 保存或修改提交记录
     *
     * @return
     */
    @ApiOperation(value = "保存或修改")
    @PostMapping("/save")
    public ApiRest save(@RequestBody SysDictType reqDTO) {
        baseService.saveData(reqDTO);
        return success();
    }

    @Resource
    SysDictDataService dictDataService;

    /**
     * 获取有效的码值, 用于初始化下来选择框
     * @param reqData 入参, 主要是码值code
     * @return 码表集合
     */
    @ApiOperation(value = "获取有效的码值")
    @PostMapping("/getDicList")
    public ApiRest<List<SysDictData>> getEffectDictByCode(@RequestBody SysDictType reqData) {
        List<SysDictData> dicDataByCode =
                baseService.getDicDataByCode(reqData.getCode(), i -> dictDataService.getListByCode(i));
        dicDataByCode.forEach(item -> item.setLabel(item.getText()));
        return success(dicDataByCode);
    }

    /**
     * 获取所有码值
     * @param reqData 入参, 主要是码值code
     * @return 码值集合
     */
    @ApiOperation(value = "获取所有码值")
    @PostMapping("/getAllDicList")
    public ApiRest<List<SysDictData>> getAllDictByCode(@RequestBody SysDictType reqData) {
        return success(baseService.getDicDataByCode(reqData.getCode(), i -> dictDataService.getAllListByCode(i)));
    }

    /**
     * 获取所有码值
     * @return 码值集合
     */
    @ApiOperation(value = "获取所有生效码值")
    @PostMapping("/initDict")
    public ApiRest<Map<String,List<SysDictData>>> initDict() {
        return success(dataService.getAllEffectDicData());
    }
}

