package com.czg.controller.admin;

import com.czg.annotation.SaAdminCheckPermission;
import com.czg.annotation.SaCheckMainShop;
import com.czg.constant.TableValueConstant;
import com.czg.market.dto.MkRedemptionConfigDTO;
import com.czg.market.dto.MkEnableConfigDTO;
import com.czg.market.service.MkRedemptionConfigService;
import com.czg.market.service.MkEnableConfigService;
import com.czg.market.vo.MkRedemptionCodeVO;
import com.czg.market.vo.MkRedemptionConfigVO;
import com.czg.market.vo.MkEnableConfigVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.validator.group.UpdateGroup;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.groups.Default;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *
 * 券兑换码相关
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/couponRedemption")
public class CouponRedemptionController {
    @Resource
    private MkRedemptionConfigService configService;
    @Resource
    private MkEnableConfigService enableConfigService;

    /**
     * 开关。可用门店修改
     */
    @SaAdminCheckPermission(parentName = "券兑换码",value = "couponRedemption:enable", name = "券兑换码状态修改")
    @PutMapping("/enable")
    public CzgResult<Boolean> upEnable(@Validated @RequestBody MkEnableConfigDTO dto) {
        return CzgResult.success(enableConfigService.upEnable(StpKit.USER.getMainShopId(),StpKit.USER.getLoginIdAsLong(), dto, TableValueConstant.EnableConfig.Type.COUPON_REDEMPTION));
    }

    /**
     * 开关状态，可用门店详情
     */
    @GetMapping("/enable/status")
    public CzgResult<MkEnableConfigVO> upEnable() {
        return CzgResult.success(enableConfigService.detail(StpKit.USER.getMainShopId(), StpKit.USER.getLoginIdAsLong(), TableValueConstant.EnableConfig.Type.COUPON_REDEMPTION));
    }

    /**
     * 配置信息详情
     */
    @SaAdminCheckPermission(parentName = "券兑换码",value = "couponRedemption:detail", name = "券兑换码详情")
    @GetMapping
    public CzgResult<MkRedemptionConfigVO> detail(@RequestParam Long id) {
        return CzgResult.success(configService.detail(StpKit.USER.getMainShopId(), id));
    }

    /**
     * 配置信息获取 列表
     */
    @SaAdminCheckPermission(parentName = "券兑换码",value = "couponRedemption:list", name = "券兑换码列表")
    @GetMapping("/list")
    public CzgResult<Page<MkRedemptionConfigVO>> list(@RequestParam(required = false, defaultValue = "-1") Integer status) {
        return CzgResult.success(configService.pageInfo(StpKit.USER.getMainShopId(), status, false));
    }

    /**
     * 配置信息添加
     */
    @SaAdminCheckPermission(parentName = "券兑换码",value = "couponRedemption:add", name = "券兑换码配置添加")
    @PostMapping
    @SaCheckMainShop
    public CzgResult<Boolean> add(@Validated @RequestBody MkRedemptionConfigDTO dto) {
        return CzgResult.success(configService.add(StpKit.USER.getMainShopId(), dto, false));
    }

    /**
     * 配置信息修改
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "券兑换码",value = "couponRedemption:edit", name = "券兑换码修改")
    @SaCheckMainShop
    @PutMapping
    public CzgResult<Boolean> edit(@Validated({UpdateGroup.class, Default.class}) @RequestBody MkRedemptionConfigDTO dto) {
        return CzgResult.success(configService.edit(StpKit.USER.getShopId(), dto, false));
    }

    /**
     * 券兑换码码列表
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "券兑换码",value = "couponRedemption:codeList", name = "券兑换码码列表")
    @GetMapping("/code/list")
    public CzgResult<Page<MkRedemptionCodeVO>> codeList(@RequestParam Long redemptionId, @RequestParam(required = false) String code, @RequestParam(required = false) Integer status) {
        return CzgResult.success(configService.codeList(StpKit.USER.getMainShopId(), redemptionId, code, status, false));
    }

    /**
     * 券兑换码码导出
     */
    @SaAdminCheckPermission(parentName = "券兑换码",value = "couponRedemption:codeExport", name = "券兑换码码列表导出")
    @GetMapping("/code/export")
    public void exportCodeList(@RequestParam Long redemptionId, @RequestParam(required = false) String code, @RequestParam(required = false) Integer status,
                                                                      HttpServletResponse response, HttpServletRequest request) {
        configService.exportCodeList(StpKit.USER.getMainShopId(), redemptionId, code, status, response, request, false);
    }

}
