package com.czg.controller.admin;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.market.dto.MkConsumeCashbackDTO;
import com.czg.market.entity.MkShopConsumeDiscountRecord;
import com.czg.market.service.MkConsumeCashbackRecordService;
import com.czg.market.service.MkConsumeCashbackService;
import com.czg.market.vo.MkConsumeCashbackVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 消费返现
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/consumeCashback")
public class ConsumeCashbackController {

    @Resource
    private MkConsumeCashbackService service;
    @Resource
    private MkConsumeCashbackRecordService consumeCashbackRecordService;

    /**
     * 配置信息获取
     * 权限标识: activate:list
     */
    @SaAdminCheckPermission(parentName = "消费返现",value = "consumeDiscount:detail", name = "消费返现详情")
    @GetMapping
    public CzgResult<MkConsumeCashbackVO> detail() {
        return CzgResult.success(service.detail(StpKit.USER.getMainShopId()));
    }

    /**
     * 配置信息修改
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "消费返现",value = "consumeDiscount:edit", name = "消费返现修改")
    @PostMapping
    public CzgResult<Boolean> edit(@Validated @RequestBody MkConsumeCashbackDTO consumeDiscountDTO) {
        return CzgResult.success(service.edit(StpKit.USER.getShopId(), consumeDiscountDTO));
    }


    /**
     * 记录获取
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "消费返现",value = "consumeDiscount:record", name = "消费返现记录")
    @GetMapping("/record")
    public CzgResult<Map<String, Object>> record(@RequestParam(required = false) Long shopId, @RequestParam(required = false) String key,
                                                 @RequestParam(required = false)String startTime, @RequestParam(required = false) String endTime) {
        return CzgResult.success(consumeCashbackRecordService.getRecord(StpKit.USER.getMainShopId(), shopId, key,
                StrUtil.isNotBlank(startTime) ? DateUtil.parseLocalDateTime(startTime) : null,  StrUtil.isNotBlank(endTime) ? DateUtil.parseLocalDateTime(endTime) : null));
    }



}
