package com.czg.controller.admin;

import com.czg.account.dto.shopuser.*;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.AShopUserService;
import com.czg.account.service.ShopUserFlowService;
import com.czg.account.vo.ShopUserFlowVO;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.enums.ShopUserFlowBizEnum;
import com.czg.market.entity.SmsPushEventUser;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * 店铺用户管理
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/shopUser")
public class ShopUserController {

    @Resource
    private AShopUserService shopUserService;
    @Resource
    private ShopUserFlowService shopUserFlowService;

    /**
     * 获取店铺用户概述信息
     *
     * @param isVip 0 非vip 1 vip
     * @return 概述信息
     */
    @SaAdminCheckPermission(parentName = "店铺用户",value = "shopUser:summary", name = "店铺用户概述信息")
    @GetMapping("/summary")
    public CzgResult<ShopUserSummaryDTO> summary(Integer isVip) {
        return CzgResult.success(shopUserService.getSummary(StpKit.USER.getHeadShopIdBySession(), isVip));
    }

    /**
     * 获取店铺用户充值记录
     * @param userId 用户id
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param bizCode 充值类型 类型：
     * cashIn 现金充值，
     * wechatIn 微信小程序充值，
     * alipayIn 支付宝小程序充值，
     * awardIn 充值奖励，
     * rechargeRefund 充值退款
     * orderPay 订单消费，
     * orderRefund 订单退款，
     * adminIn 管理员充值
     * adminOut管理员消费
     * @return 充值记录
     */
    @SaAdminCheckPermission(parentName = "店铺用户",value = "shopUser:flow", name = "店铺用户充值记录")
    @GetMapping("/flow")
    public CzgResult<Page<ShopUserFlowVO>> flow(Integer userId, String bizCode, String startTime, String endTime) {
        return CzgResult.success(shopUserFlowService.pageInfo(StpKit.USER.getShopId(), userId, bizCode, startTime, endTime));
    }

    /**
     * 余额明细记录导出
     * @param userId 用户id
     * @param bizCode 代码
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param response 响应
     * @throws IOException 异常
     */
    @SaAdminCheckPermission(parentName = "店铺用户",value = "shopUser:flow:downLoad", name = "店铺用户充值记录导出")
    @GetMapping("/flow/download")
    public void flowDownload(Integer userId, String bizCode, String startTime, String endTime, HttpServletResponse response) throws IOException {
        shopUserFlowService.flowDownload(StpKit.USER.getHeadShopIdBySession(), userId, bizCode, startTime, endTime, response);
    }

    /**
     * 获取店铺用户列表
     *
     * @param key   昵称或手机号
     * @param isVip 0 非vip 1 vip
     * @return 用户列表
     */
    @SaAdminCheckPermission(parentName = "店铺用户",value = "shopUser:list", name = "店铺用户列表")
    @GetMapping
    public CzgResult<Page<ShopUserDTO>> list(String key, Integer isVip, BigDecimal amount) {
        return CzgResult.success(shopUserService.getPage(key, isVip, amount));
    }

    /**
     * 导出用户列表
     *
     * @param key   昵称或手机号
     * @param isVip 0 非vip 1 vip
     */
    @GetMapping("/export")
    public void exportUserList(String key, Integer isVip, HttpServletResponse response) {
        shopUserService.exportUserList(key, isVip, response);
    }

    @GetMapping("/getPage")
    public CzgResult<Page<ShopUser>> getPage(@RequestParam(required = false)String key,@RequestParam(required = false) Integer isVip) {
        return CzgResult.success(shopUserService.getPage(key, isVip));
    }

    /**
     * 获取店铺用户详情
     * @param id 会员用户id
     * @param userId 用户id
     * @return 用户详情
     */
    @SaAdminCheckPermission(parentName = "店铺用户",value = "shopUser:detail", name = "店铺用户详情")
    @GetMapping("/detail")
    public CzgResult<ShopUser> detail(Integer id, Integer userId) {
        if (id == null && userId == null) {
            return CzgResult.failure("id和userId不能重复为空");
        }
        return CzgResult.success(shopUserService.getDetail(id, userId));
    }

    /**
     * 店铺用户信息添加
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "店铺用户",value = "shopUser:add", name = "店铺用户添加")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated ShopUserAddDTO shopUserAddDTO) {
        return CzgResult.success(shopUserService.add(StpKit.USER.getHeadShopIdBySession(), shopUserAddDTO));
    }

    /**
     * 店铺用户信息修改
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "店铺用户",value = "shopUser:edit", name = "店铺用户修改")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated ShopUserEditDTO shopUserEditDTO) {
        return CzgResult.success(shopUserService.updateInfo(StpKit.USER.getHeadShopIdBySession(), shopUserEditDTO));
    }

    /**
     * 店铺会员信息修改
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "店铺用户",value = "shopUser:edit", name = "店铺用户修改")
    @PutMapping("/editVip")
    public CzgResult<Boolean> editVip(@RequestBody @Validated ShopUserVipEditDTO vipEditDTO) {
        return CzgResult.success(shopUserService.editVip(vipEditDTO));
    }

    /**
     * 店铺用户余额修改
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "店铺用户",value = "shopUser:editFlow", name = "店铺用户余额修改")
    @PutMapping("/money")
    public CzgResult<Boolean> editMoney(@RequestBody @Validated ShopUserMoneyEditDTO shopUserMoneyEditDTO) {
        shopUserMoneyEditDTO.setBizEnum(ShopUserFlowBizEnum.ADMIN_IN);
        return CzgResult.success(shopUserService.updateMoney(shopUserMoneyEditDTO) > 0L);
    }

    /**
     * 获取营销推送任务用户列表
     * @param smsPushEventUser 营销推送任务用户
     * @return 用户列表
     */
    @PostMapping("/getPushEventUser")
    public CzgResult<Page<ShopUser>> getPushEventUser(@RequestBody @Validated SmsPushEventUser smsPushEventUser) {
        AssertUtil.isNull(smsPushEventUser.getShopId(), "店铺id不能为空");
        return CzgResult.success(shopUserService.getPushEventUser(smsPushEventUser));
    }

    /**
     * 获取公众号推送任务用户列表
     * @param pushEventUser 公众号推送任务用户
     * @return 用户列表
     */
    @PostMapping("/getAcPushEventUser")
    public CzgResult<Page<ShopUser>> getAcPushEventUser(@RequestBody @Validated SmsPushEventUser pushEventUser) {
        AssertUtil.isNull(pushEventUser.getShopId(), "店铺id不能为空");
        return CzgResult.success(shopUserService.getAcPushEventUser(pushEventUser));
    }

}
