package com.ruoyi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.controller.req.PlatformRulesPageReq;
import com.ruoyi.frequency.platformrules.entity.PlatformRules;
import com.ruoyi.frequency.platformrules.service.PlatformRulesService;
import com.ruoyi.frequency.platformrulestype.entity.PlatformRulesType;
import com.ruoyi.frequency.platformrulestype.service.PlatformRulesTypeService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.BodyIdReq;
import com.ruoyi.vo.RyController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/platformRules")
@Api(tags = "平台规则")
public class ApiPlatformRulesController extends RyController {

    @Autowired
    private PlatformRulesService platformRulesService;


    @Autowired
    private PlatformRulesTypeService platformRulesTypeService;

    @PostMapping("/rulesTypeList/{userType}")
    @ApiOperation(value = "平台规则分类列表", notes = "平台规则分类列表")
    public ResponseData<List<PlatformRulesType>> rulesTypeList(@PathVariable Integer userType) {
        List<PlatformRulesType> list = platformRulesTypeService.list(new QueryWrapper<PlatformRulesType>().lambda()
                .eq(PlatformRulesType::getDelFlag, PublicCommon.启用)
                .eq(PlatformRulesType::getUserType, userType)
                .orderByAsc(PlatformRulesType::getSort));
        return ResponseData.success(list);
    }

    @PostMapping("/platformRulesPage")
    @ApiOperation(value = "平台规则列表", notes = "平台规则列表")
    public ResponseData<PageResult<PlatformRules>> platformRulesPage(@RequestBody PlatformRulesPageReq req) {
        Page<PlatformRules> page = platformRulesService.page(new Page<>(req.getPageNo(), req.getPageSize()), new QueryWrapper<PlatformRules>().lambda()
                .eq(PlatformRules::getUserType, req.getUserType())
                .eq(PlatformRules::getDelFlag, PublicCommon.启用)
                .eq(ObjectUtil.isNotEmpty(req.getPlatformRulesTypeId()), PlatformRules::getPlatformRulesTypeId, req.getPlatformRulesTypeId())
                .orderByAsc(PlatformRules::getSort)
        );
        if (page.getRecords().size() > 0) {
            page.getRecords().stream().forEach(s -> s.setContent(""));
        }
        return ResponseData.success(new PageResult<>(page));
    }

    @PostMapping("/platformRulesDetail")
    @ApiOperation(value = "平台规则详情", notes = "平台规则详情")
    public ResponseData<PlatformRules> appealDetail(@RequestBody @Valid BodyIdReq req) {
        PlatformRules platformRules = platformRulesService.getById(req.getId());
        PlatformRulesType platformRulesType = platformRulesTypeService.getById(platformRules.getPlatformRulesTypeId());
        if (platformRulesType != null) {
            platformRules.setPlatformRulesTypeName(platformRulesType.getName());
        }
        return ResponseData.success(platformRules);
    }

}
