package co.yixiang.yshop.module.member.controller.admin.integralrule;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import co.yixiang.yshop.module.member.controller.admin.integralrule.vo.*;
import co.yixiang.yshop.module.member.convert.integralrule.IntegralRuleConvert;
import co.yixiang.yshop.module.member.dal.dataobject.integralrule.IntegralRuleDO;
import co.yixiang.yshop.module.member.service.integralrule.IntegralRuleService;
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.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 会员积分规则")
@RestController
@RequestMapping("/member/integral-rule")
@Validated
public class IntegralRuleController {

    @Resource
    private IntegralRuleService integralRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建会员积分规则")
    @PreAuthorize("@ss.hasPermission('member:integral-rule:create')")
    public CommonResult<Long> createIntegralRule(@Valid @RequestBody IntegralRuleCreateReqVO createReqVO) {
        return success(integralRuleService.createIntegralRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员积分规则")
    @PreAuthorize("@ss.hasPermission('member:integral-rule:update')")
    public CommonResult<Boolean> updateIntegralRule(@Valid @RequestBody IntegralRuleUpdateReqVO updateReqVO) {
        integralRuleService.updateIntegralRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员积分规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:integral-rule:delete')")
    public CommonResult<Boolean> deleteIntegralRule(@RequestParam("id") Long id) {
        integralRuleService.deleteIntegralRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员积分规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:integral-rule:query')")
    public CommonResult<IntegralRuleRespVO> getIntegralRule(@RequestParam("id") Long id) {
        IntegralRuleDO integralRule = integralRuleService.getIntegralRule(id);
        return success(IntegralRuleConvert.INSTANCE.convert(integralRule));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员积分规则列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:integral-rule:query')")
    public CommonResult<List<IntegralRuleRespVO>> getIntegralRuleList(@RequestParam("ids") Collection<Long> ids) {
        List<IntegralRuleDO> list = integralRuleService.getIntegralRuleList(ids);
        return success(IntegralRuleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员积分规则分页")
    @PreAuthorize("@ss.hasPermission('member:integral-rule:query')")
    public CommonResult<PageResult<IntegralRuleRespVO>> getIntegralRulePage(@Valid IntegralRulePageReqVO pageVO) {
        PageResult<IntegralRuleDO> pageResult = integralRuleService.getIntegralRulePage(pageVO);
        return success(IntegralRuleConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员积分规则 Excel")
    @PreAuthorize("@ss.hasPermission('member:integral-rule:export')")
    @OperateLog(type = EXPORT)
    public void exportIntegralRuleExcel(@Valid IntegralRuleExportReqVO exportReqVO,
                                        HttpServletResponse response) throws IOException {
        List<IntegralRuleDO> list = integralRuleService.getIntegralRuleList(exportReqVO);
        // 导出 Excel
        List<IntegralRuleExcelVO> datas = IntegralRuleConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员积分规则.xls", "数据", IntegralRuleExcelVO.class, datas);
    }

    @GetMapping("getRule")
    @Operation(summary = "查询会员积分规则")
    @PreAuthorize("@ss.hasPermission('member:integral-rule:query')")
    public CommonResult<IntegralRuleVO> getRule() {
        IntegralRuleVO vo = integralRuleService.getRule();
        return CommonResult.success(vo);
    }

    @PostMapping("saveRule")
    @Operation(summary = "保存会员积分规则")
    @PreAuthorize("@ss.hasPermission('member:integral-rule:update')")
    public CommonResult<Boolean> saveRule(@RequestBody IntegralRuleVO vo) {
        integralRuleService.saveRule(vo);
        return CommonResult.success(true);
    }
}
