package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.annotation.SaAdminCheckRole;
import com.czg.market.dto.SmsMoneyDetailDto;
import com.czg.market.dto.SmsShopMoneyDetailDTO;
import com.czg.market.dto.SmsShopTemplateDTO;
import com.czg.market.dto.SmsTemplateQueryDTO;
import com.czg.market.entity.SmsShopMoney;
import com.czg.market.entity.SmsShopMoneyDetail;
import com.czg.market.service.SmsShopMoneyDetailService;
import com.czg.market.service.SmsShopMoneyService;
import com.czg.market.service.SmsShopTemplateService;
import com.czg.market.vo.SmsShopMoneyVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.service.market.mapper.SmsShopMoneyMapper;
import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 短信模板
 *
 * @author ww
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class SmsShopTemplateController {
    @Resource
    private SmsShopTemplateService templateService;
    @Resource
    private SmsShopMoneyDetailService smsMoneyDetailService;
    @Resource
    private SmsShopMoneyService shopMoneyService;

    /**
     * 列表
     */
    @GetMapping("/smsTemplate")
    public CzgResult<List<SmsShopTemplateDTO>> getTemplateList(@RequestParam(required = false) String title,
                                                               @RequestParam(required = false) Integer status) {
        List<SmsShopTemplateDTO> data = templateService.getTemplateList(title, StpKit.USER.getShopId(), status);
        return CzgResult.success(data);
    }

    /**
     * 平台 商家申请记录
     */
    @SaAdminCheckRole("管理员")
    @PostMapping("/smsTemplate/query")
    public CzgResult<Page<SmsShopTemplateDTO>> getTemplateList2(@RequestBody SmsTemplateQueryDTO param) {
        Page<SmsShopTemplateDTO> data = templateService.getTemplateList2(param);
        return CzgResult.success(data);
    }


    /**
     * 平台 模板状态/删除
     */
    @SaAdminCheckRole("管理员")
    @PostMapping("/smsTemplate/shopUse")
    public CzgResult<Void> shopUse(@RequestBody SmsShopTemplateDTO param) {
        templateService.shopUse(param);
        return CzgResult.success();
    }


    /**
     * 平台 获取店铺短信余额明细
     */
    @SaAdminCheckRole("管理员")
    @PostMapping("/smsMoneyDetail/query")
    public CzgResult<Page<SmsShopMoneyDetailDTO>> getSmsMoneyDetail2(@RequestBody SmsMoneyDetailDto param) {
        Page<SmsShopMoneyDetailDTO> data = smsMoneyDetailService.getSmsMoneyDetail2(param);
        return CzgResult.success(data);
    }


    /**
     * 新增
     */
    @PostMapping("/smsTemplate")
    public CzgResult<Void> addTemplate(@RequestBody @Validated(InsertGroup.class) SmsShopTemplateDTO param) {
        param.setShopId(StpKit.USER.getShopId());
        templateService.addTemplate(param);
        return CzgResult.success();
    }

    /**
     * 重新提交
     * 状态为 -1 失败的 可以修改模板重新提交
     */
    @PostMapping("/smsTemplate/resubmit")
    public CzgResult<Void> resubmit(@RequestBody @Validated(UpdateGroup.class) SmsShopTemplateDTO param) {
        param.setShopId(StpKit.USER.getShopId());
        templateService.resubmit(param);
        return CzgResult.success();
    }

    /**
     * 获取店铺短信余额
     */
    @GetMapping("/smsMoney")
    public CzgResult<SmsShopMoney> getShopSmsMoney() {
        SmsShopMoney shopMoney = shopMoneyService.getShopMoney(StpKit.USER.getShopId());
        return CzgResult.success(shopMoney);
    }

    /**
     * 平台：短信店铺配置
     */
    @SaAdminCheckRole("管理员")
    @GetMapping("/smsMoney/page")
    public CzgResult<Page<SmsShopMoneyVO>> getShopSmsMoneyPage(@RequestParam(required = false) Integer page,
                                                               @RequestParam(required = false) Integer size,
                                                               @RequestParam(required = false) String name
    ) {
        Page<SmsShopMoneyVO> data = shopMoneyService.getShopMoneyPage(page, size, name);
        return CzgResult.success(data);
    }


    /**
     * 变更店铺短信余额
     */
    @PostMapping("/smsMoney/change")
    public CzgResult<Void> change(@RequestBody SmsShopMoneyDetailDTO money) {
//        Long shopId = StpKit.USER.getShopId();
        SmsShopMoney shopMoney = shopMoneyService.getShopMoney(money.getShopId());
        SmsShopMoneyDetail moneyDetail = BeanUtil.toBean(money, SmsShopMoneyDetail.class);
        moneyDetail.setShopId(money.getShopId());
        if (money.getType() == 1) {
            shopMoney.setMoney(shopMoney.getMoney().add(money.getExpense()));
        } else {
            shopMoney.setMoney(shopMoney.getMoney().subtract(money.getExpense()));
        }
        moneyDetail.setBalance(shopMoney.getMoney());
        moneyDetail.setSendRows(0L);
        smsMoneyDetailService.save(moneyDetail);
        shopMoneyService.updateById(shopMoney);
        return CzgResult.success();
    }


    /**
     * 获取店铺短信余额明细
     */
    @GetMapping("/smsMoneyDetail")
    public CzgResult<JSONObject> getSmsMoneyDetailPage(@RequestParam(required = false) Integer page,
                                                       @RequestParam(required = false) Integer size) {
        Page<SmsShopMoneyDetailDTO> data = smsMoneyDetailService.getSmsMoneyDetailPage(StpKit.USER.getShopId());
        JSONObject from = JSONObject.from(data);
        from.put("sendTotal", smsMoneyDetailService.countSendTotal(StpKit.USER.getShopId()));
        from.put("sendAmountTotal", smsMoneyDetailService.countSendAmountTotal(StpKit.USER.getShopId()));
        return CzgResult.success(from);
    }
}
