package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.dto.ShopPayTypeDTO;
import com.czg.account.dto.paytype.ShopPayTypeAddDTO;
import com.czg.account.entity.ShopPayType;
import com.czg.account.service.ShopPayTypeService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.validator.group.InsertGroup;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 支付方式管理
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/payType")
public class ShopPayTypeController {
    @Resource
    private ShopPayTypeService shopPayTypeService;

    /**
     * 支付方式列表
     */
//    @SaAdminCheckPermission(parentName = "支付方式", value = "shopPayType:list", name = "支付方式列表")
    @GetMapping
    public CzgResult<List<ShopPayType>> list() {
        return CzgResult.success(shopPayTypeService.getList(StpKit.USER.getShopId()));
    }

    /**
     * 支付方式详情获取
     */
    @SaAdminCheckPermission(parentName = "支付方式", value = "shopPayType:detail", name = "支付方式详情获取")
    @GetMapping("/detail")
    public CzgResult<ShopPayType> detail(@RequestParam Integer id) {
        return CzgResult.success(shopPayTypeService.getOne(new QueryWrapper().eq(ShopPayType::getId, id)));
    }

    /**
     * 支付方式编辑
     */
    @SaAdminCheckPermission(parentName = "支付方式", value = "shopPayType:edit", name = "支付方式编辑")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody ShopPayTypeDTO shopPayTypeDTO) {
        return CzgResult.success(shopPayTypeService.edit(StpKit.USER.getShopId(), shopPayTypeDTO));
    }

    /**
     * 支付方式添加
     */
    @SaAdminCheckPermission(parentName = "支付方式", value = "shopPayType:save", name = "支付方式添加")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated(InsertGroup.class) ShopPayTypeAddDTO shopPayTypeAddDTO) {
        ShopPayType shopPayType = BeanUtil.copyProperties(shopPayTypeAddDTO, ShopPayType.class);
        return CzgResult.success(shopPayTypeService.add(StpKit.USER.getShopId(), shopPayType));
    }

    /**
     * 支付类型拖动排序
     *
     * @param shopId     店铺ID
     * @param id         拖动的支付类型ID
     * @param targetSort 目标位置排序号
     */
    @GetMapping("/sort")
    public CzgResult<Void> sort(@RequestParam Long shopId, @RequestParam Integer id, @RequestParam Integer targetSort) {
        shopPayTypeService.updateSort(shopId, id, targetSort);
        return CzgResult.success();
    }
}
