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

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.problem.vo.ProblemPageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.problem.vo.ProblemRespVO;
import com.yc.ship.module.miniapplet.controller.admin.problem.vo.ProblemSaveReqVO;
import com.yc.ship.module.miniapplet.convert.problemtype.ProblemTypeConvert;
import com.yc.ship.module.miniapplet.dal.dataobject.problem.ProblemDO;
import com.yc.ship.module.miniapplet.dal.dataobject.problemtype.ProblemTypeDO;
import com.yc.ship.module.miniapplet.service.problem.ProblemService;
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")
@Validated
public class ProblemController {

    @Resource
    private ProblemService problemService;

    @Resource
    private ProblemTypeService problemTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建问题管理")
    @PreAuthorize("@ss.hasPermission('applet:problem:create')")
    public CommonResult<Long> createProblem(@Valid @RequestBody ProblemSaveReqVO createReqVO) {
        return success(problemService.createProblem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新问题管理")
    @PreAuthorize("@ss.hasPermission('applet:problem:update')")
    public CommonResult<Boolean> updateProblem(@Valid @RequestBody ProblemSaveReqVO updateReqVO) {
        problemService.updateProblem(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得问题管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('applet:problem:query')")
    public CommonResult<ProblemRespVO> getProblem(@RequestParam("id") Long id) {
        ProblemDO problem = problemService.getProblem(id);
        return success(BeanUtils.toBean(problem, ProblemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得问题管理分页")
    @PreAuthorize("@ss.hasPermission('applet:problem:query')")
    public CommonResult<PageResult<ProblemRespVO>> getProblemPage(@Valid ProblemPageReqVO pageReqVO) {
        PageResult<ProblemDO> pageResult = problemService.getProblemPage(pageReqVO);
        List<ProblemTypeDO> otherList = problemTypeService.getProblemTypeList(convertList(pageResult.getList(), ProblemDO::getTypeId));
        Map<Long, ProblemTypeDO> map = CollectionUtils.convertMap(otherList, ProblemTypeDO::getId);
        return success(new PageResult<>(ProblemTypeConvert.INSTANCE.convertProblemList(pageResult.getList(), map), pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出问题管理 Excel")
    @PreAuthorize("@ss.hasPermission('applet:problem:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportProblemExcel(@Valid ProblemPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProblemDO> list = problemService.getProblemPage(pageReqVO).getList();
        List<ProblemTypeDO> otherList = problemTypeService.getProblemTypeList(convertList(list, ProblemDO::getTypeId));
        Map<Long, ProblemTypeDO> map = CollectionUtils.convertMap(otherList, ProblemTypeDO::getId);
        List<ProblemRespVO> respVOS = ProblemTypeConvert.INSTANCE.convertProblemList(list, map);
        // 导出 Excel
        ExcelUtils.write(response, "问题管理.xls", "数据", ProblemRespVO.class, respVOS);
    }

}
