package com.czg.controller.admin;

import cn.hutool.core.thread.ThreadUtil;
import com.czg.log.annotation.OperationLog;
import com.czg.product.dto.ShopVendorBillPayDTO;
import com.czg.product.dto.ShopVendorDTO;
import com.czg.product.service.ShopSyncService;
import com.czg.product.service.ShopVendorService;
import com.czg.product.vo.ShopVendorBillPayRecordVO;
import com.czg.product.vo.ShopVendorBillRecordVO;
import com.czg.product.vo.ShopVendorBillVO;
import com.czg.product.vo.ShopVendorSummaryVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.czg.validator.group.DefaultGroup;
import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 供应商
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-20
 */
@RestController
@RequestMapping("/admin/product/vendor")
public class ShopVendorController {

    @Resource
    private ShopVendorService shopVendorService;

    @Resource
    private ShopSyncService shopSyncService;

    /**
     * 分页
     */
    @GetMapping("page")
    @OperationLog("供应商-分页")
    //@SaAdminCheckPermission("shopVendor:page")
    public CzgResult<Page<ShopVendorDTO>> getShopVendorPage(ShopVendorDTO param) {
        Page<ShopVendorDTO> data = shopVendorService.getShopVendorPage(param);
        return CzgResult.success(data);
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @OperationLog("供应商-列表")
    //@SaAdminCheckPermission("shopVendor:list")
    public CzgResult<List<ShopVendorDTO>> getShopVendorList(ShopVendorDTO param) {
        List<ShopVendorDTO> data = shopVendorService.getShopVendorList(param);
        return CzgResult.success(data);
    }

    /**
     * 详情
     * param id 供应商id
     */
    @GetMapping("{id}")
    @OperationLog("供应商-详情")
    //@SaAdminCheckPermission("shopVendor:info")
    public CzgResult<ShopVendorDTO> getShopVendorById(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "{}不能为空", "id");
        ShopVendorDTO data = shopVendorService.getShopVendorById(id);
        return CzgResult.success(data);
    }

    /**
     * 新增
     */
    @PostMapping
    @OperationLog("供应商-新增")
    //@SaAdminCheckPermission("shopVendor:add")
    public CzgResult<Void> addShopVendor(@RequestBody @Validated({InsertGroup.class, DefaultGroup.class}) ShopVendorDTO dto) {
        shopVendorService.addShopVendor(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @OperationLog("供应商-修改")
    //@SaAdminCheckPermission("shopVendor:update")
    public CzgResult<Void> updateShopVendor(@RequestBody @Validated({UpdateGroup.class, DefaultGroup.class}) ShopVendorDTO dto) {
        shopVendorService.updateShopVendor(dto);
        asyncToBranchShop(dto.getId());
        return CzgResult.success();
    }

    /**
     * 删除
     *
     * @param id 供应商id
     */
    @DeleteMapping("{id}")
    @OperationLog("供应商-删除")
    //@SaAdminCheckPermission("shopVendor:delete")
    public CzgResult<Void> deleteShopVendor(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "id");
        shopVendorService.deleteShopVendor(id);
        asyncToBranchShop(id);
        return CzgResult.success();
    }

    /**
     * 供应商账单统计
     *
     * @return 统计信息
     */
    @GetMapping("summary")
//    @OperationLog("供应商账单-统计")
    public CzgResult<ShopVendorSummaryVO> summary() {
        return CzgResult.success(shopVendorService.summary(StpKit.USER.getShopId()));
    }

    /**
     * 账单列表
     *
     * @return 账单列表
     */
    @GetMapping("/bill")
//    @OperationLog("供应商账单-列表")
    public CzgResult<Page<ShopVendorBillVO>> bill(@RequestParam(required = false) String key) {
        return CzgResult.success(shopVendorService.billList(StpKit.USER.getShopId(), key));
    }

    /**
     * 账单记录
     *
     * @return 记录list
     */
    @GetMapping("/bill/record")
//    @OperationLog("供应商账单-列表")
    public CzgResult<Page<ShopVendorBillRecordVO>> bill(@RequestParam Integer vendorId, @RequestParam(required = false) String key) {
        return CzgResult.success(shopVendorService.billRecord(StpKit.USER.getShopId(), vendorId, key));
    }


    /**
     * 账单支付
     *
     * @return 记录list
     */
    @PostMapping("/bill/pay")
//    @OperationLog("供应商账单-付款")
    public CzgResult<Boolean> pay(@RequestBody @Validated ShopVendorBillPayDTO payDTO) {
        return CzgResult.success(shopVendorService.pay(StpKit.USER.getShopId(), payDTO));
    }


    /**
     * 账单支付记录
     *
     * @return 记录list
     */
    @GetMapping("/bill/pay/record")
//    @OperationLog("供应商账单-付款记录")
    public CzgResult<Page<ShopVendorBillPayRecordVO>> payRecord(@RequestParam Long flowId) {
        return CzgResult.success(shopVendorService.payRecord(StpKit.USER.getShopId(), flowId));
    }

    private void asyncToBranchShop(Long id) {
        long shopId = StpKit.USER.getShopId(0L);
        long sysUserId = StpKit.USER.getLoginIdAsLong();
        ThreadUtil.execAsync(() -> {
            shopSyncService.syncVendorBySourceShop(shopId, id, sysUserId);
        });
    }

}
