package com.yc.ship.module.miniapplet.controller.admin.problemtype;

import com.yc.ship.framework.common.pojo.CommonResult;

import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.miniapplet.controller.admin.problemtype.vo.ProblemTypePageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.problemtype.vo.ProblemTypeRespVO;
import com.yc.ship.module.miniapplet.controller.admin.problemtype.vo.ProblemTypeSaveReqVO;
import com.yc.ship.module.miniapplet.controller.admin.problemtype.vo.ProblemTypeSimpleRespVO;
import com.yc.ship.module.miniapplet.convert.problemtype.ProblemTypeConvert;
import com.yc.ship.module.miniapplet.dal.dataobject.problemtype.ProblemTypeDO;
import com.yc.ship.module.miniapplet.service.problemtype.ProblemTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.List;
import java.util.Map;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.common.util.collection.CollectionUtils.convertList;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 问题类型")
@RestController
@RequestMapping("/applet/problem-type")
@Validated
public class ProblemTypeController {

    @Resource
    private ProblemTypeService problemTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建问题类型")
    @PreAuthorize("@ss.hasPermission('applet:problem-type:create')")
    public CommonResult<Long> createProblemType(@Valid @RequestBody ProblemTypeSaveReqVO createReqVO) {
        return success(problemTypeService.createProblemType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新问题类型")
    @PreAuthorize("@ss.hasPermission('applet:problem-type:update')")
    public CommonResult<Boolean> updateProblemType(@Valid @RequestBody ProblemTypeSaveReqVO updateReqVO) {
        problemTypeService.updateProblemType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除问题类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('applet:problem-type:delete')")
    public CommonResult<Boolean> deleteProblemType(@RequestParam("id") Long id) {
        problemTypeService.deleteProblemType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得问题类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('applet:problem-type:query')")
    public CommonResult<ProblemTypeRespVO> getProblemType(@RequestParam("id") Long id) {
        ProblemTypeDO problemType = problemTypeService.getProblemType(id);
        return success(BeanUtils.toBean(problemType, ProblemTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得问题类型分页")
    @PreAuthorize("@ss.hasPermission('applet:problem-type:query')")
    public CommonResult<PageResult<ProblemTypeRespVO>> getProblemTypePage(@Valid ProblemTypePageReqVO pageReqVO) {
        PageResult<ProblemTypeDO> pageResult = problemTypeService.getProblemTypePage(pageReqVO);
        List<ProblemTypeDO> otherList = problemTypeService.getProblemTypeList(convertList(pageResult.getList(), ProblemTypeDO::getParentId));
        Map<Long, ProblemTypeDO> map = CollectionUtils.convertMap(otherList, ProblemTypeDO::getId);
        return success(new PageResult<>(ProblemTypeConvert.INSTANCE.convertProblemTypeList(pageResult.getList(), map), pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出问题类型 Excel")
    @PreAuthorize("@ss.hasPermission('applet:problem-type:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportProblemTypeExcel(@Valid ProblemTypePageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProblemTypeDO> list = problemTypeService.getProblemTypePage(pageReqVO).getList();
        List<ProblemTypeDO> otherList = problemTypeService.getProblemTypeList(convertList(list, ProblemTypeDO::getParentId));
        Map<Long, ProblemTypeDO> map = CollectionUtils.convertMap(otherList, ProblemTypeDO::getId);
        List<ProblemTypeRespVO> respVOS = ProblemTypeConvert.INSTANCE.convertProblemTypeList(list, map);
        // 导出 Excel
        ExcelUtils.write(response, "问题类型.xls", "数据", ProblemTypeRespVO.class, respVOS);
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取精简信息列表", description = "只包含被开启的点位类型，主要用于前端的下拉选项")
    public CommonResult<List<ProblemTypeSimpleRespVO>> getSimpleList(ProblemTypePageReqVO pageReqVO) {
        List<ProblemTypeDO> list = problemTypeService.getProblemTypeListByStatus(pageReqVO.getSystemType(), pageReqVO.getIsUse());
        return success(BeanUtils.toBean(list, ProblemTypeSimpleRespVO.class));
    }

}
