package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.entity.Goods;
import com.gxwebsoft.shop.entity.MerchantPackageGoods;
import com.gxwebsoft.shop.entity.UserGoodsInMerchant;
import com.gxwebsoft.shop.param.MerchantPackageGoodsParam;
import com.gxwebsoft.shop.service.GoodsService;
import com.gxwebsoft.shop.service.MerchantGoodsPackageService;
import com.gxwebsoft.shop.entity.MerchantGoodsPackage;
import com.gxwebsoft.shop.param.MerchantGoodsPackageParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.MerchantPackageGoodsService;
import com.gxwebsoft.shop.service.UserGoodsInMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 商家充值套餐控制器
 *
 * @author 科技小王子
 * @since 2024-11-20 01:55:04
 */
@Api(tags = "商家充值套餐管理")
@RestController
@RequestMapping("/api/shop/merchant-goods-package")
public class MerchantGoodsPackageController extends BaseController {
    @Resource
    private MerchantGoodsPackageService merchantGoodsPackageService;
    @Resource
    private MerchantPackageGoodsService merchantPackageGoodsService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private UserGoodsInMerchantService userGoodsInMerchantService;

    @ApiOperation("分页查询商家充值套餐")
    @GetMapping("/page")
    public ApiResult<PageResult<MerchantGoodsPackage>> page(MerchantGoodsPackageParam param) {
        // 使用关联查询
        return success(merchantGoodsPackageService.pageRel(param));
    }

    @ApiOperation("查询全部商家充值套餐")
    @GetMapping()
    public ApiResult<List<MerchantGoodsPackage>> list(MerchantGoodsPackageParam param) {
        // 使用关联查询
        List<MerchantGoodsPackage> list = merchantGoodsPackageService.listRel(param);
        for (MerchantGoodsPackage merchantGoodsPackage : list) {
            List<MerchantPackageGoods> merchantPackageGoodsList = merchantPackageGoodsService.listByPackage(merchantGoodsPackage.getId());
            List<Goods> goodsList = goodsService.listByIds(merchantPackageGoodsList.stream().map(MerchantPackageGoods::getGoodsId).toList());
            merchantGoodsPackage.setGoodsList(goodsList);
            if (param.getWithUserPackage() != null) {
                merchantGoodsPackage.setUserPackage(userGoodsInMerchantService.infoInMerchant(merchantGoodsPackage.getId(), getLoginUserId()));
            }
        }
        return success(list);
    }

    @PreAuthorize("hasAuthority('shop:merchantGoodsPackage:list')")
    @ApiOperation("根据id查询商家充值套餐")
    @GetMapping("/{id}")
    public ApiResult<MerchantGoodsPackage> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(merchantGoodsPackageService.getByIdRel(id));
    }

    @ApiOperation("添加商家充值套餐")
    @PostMapping()
    public ApiResult<?> save(@RequestBody MerchantGoodsPackage merchantGoodsPackage) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
            merchantGoodsPackage.setUserId(loginUser.getUserId());
        }
        if (merchantGoodsPackageService.save(merchantGoodsPackage)) {
            List<MerchantPackageGoods> merchantPackageGoodsList = new ArrayList<>();
            for (Integer goodsId : merchantGoodsPackage.getGoodsIdList()) {
                MerchantPackageGoods merchantPackageGoods = new MerchantPackageGoods();
                merchantPackageGoods.setGoodsId(goodsId);
                merchantPackageGoods.setPackageId(merchantGoodsPackage.getId());
                merchantPackageGoods.setUserId(getLoginUserId());
                merchantPackageGoodsList.add(merchantPackageGoods);
            }
            merchantPackageGoodsService.saveBatch(merchantPackageGoodsList);
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改商家充值套餐")
    @PutMapping()
    public ApiResult<?> update(@RequestBody MerchantGoodsPackage merchantGoodsPackage) {
        if (merchantGoodsPackageService.updateById(merchantGoodsPackage)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除商家充值套餐")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (merchantGoodsPackageService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加商家充值套餐")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<MerchantGoodsPackage> list) {
        if (merchantGoodsPackageService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改商家充值套餐")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<MerchantGoodsPackage> batchParam) {
        if (batchParam.update(merchantGoodsPackageService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除商家充值套餐")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (merchantGoodsPackageService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
