package org.springblade.modules.api.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.common.base.BaseEntity;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.business.entity.FacilityCargoWay;
import org.springblade.modules.business.service.IFacilityCargoWayService;
import org.springblade.modules.business.vo.FacilityCargoWayVO;
import org.springblade.modules.business.wrapper.FacilityCargoWayWrapper;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.mall.service.IGoodsInfoService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping(CommonConstant.API_URL + "/cargo-way")
@Api(value = "货道管理接口", tags = "货道管理接口")
public class ApiFacilityCargoWayController {

    private final IFacilityCargoWayService iFacilityCargoWayService;
    private final IGoodsInfoService iGoodsInfoService;

    /**
     * 设置货道商品
     */
    @PostMapping("/goods/setting")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "设置货道商品", notes = "传入facilityCargoWay")
    public R<Void> submit(@Valid @RequestBody FacilityCargoWay facilityCargoWay) {
        FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(facilityCargoWay.getId());
        Assert.notNull(cargoWay, "未查询到货道信息");
        if (facilityCargoWay.getGoodsStore() > cargoWay.getVolume()) {
            return R.fail("商品数量不能大于货道容量");
        }
        return R.status(iFacilityCargoWayService.saveOrUpdate(facilityCargoWay));
    }

    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查询货道列表", notes = "传入facilityCargoWay")
    public R<List<FacilityCargoWayVO>> list(@ApiParam(value = "设备id", required = true) @RequestParam Long facilityId) {
        List<FacilityCargoWay> list = iFacilityCargoWayService.list(Wrappers.lambdaQuery(FacilityCargoWay.class).eq(FacilityCargoWay::getFacilityId, facilityId));
        List<FacilityCargoWayVO> vos = FacilityCargoWayWrapper.build().listVO(list);
        if (CollUtil.isNotEmpty(vos)) {
            Set<Long> collect = vos.stream().map(FacilityCargoWay::getGoodsId).collect(Collectors.toSet());
            List<GoodsInfo> goodsInfos = iGoodsInfoService.list(Wrappers.lambdaQuery(GoodsInfo.class).in(BaseEntity::getId, collect));
            Map<Long, GoodsInfo> map = goodsInfos.stream().collect(Collectors.toMap(BaseEntity::getId, Function.identity()));
            for (FacilityCargoWayVO vo : vos) {
                GoodsInfo goodsInfo = map.get(vo.getGoodsId());
                if (goodsInfo != null) {
                    vo.setGoodsName(goodsInfo.getGoodsName());
                    vo.setGoodsCover(goodsInfo.getGoodsCover());
                }
            }
        }
        return R.data(vos);
    }

    @PostMapping("/discount/setting")
    @ApiOperationSupport(order =3 )
    @ApiOperation(value = "批量设置折扣", notes = "传入facilityCargoWay")
    public R<Void> discountSetting(@RequestBody FacilityCargoWayVO facilityCargoWay) {
        List<FacilityCargoWay> list = iFacilityCargoWayService.list(Wrappers.lambdaQuery(FacilityCargoWay.class)
                .in(BaseEntity::getId, facilityCargoWay.getCargoWayIds()));
        for (FacilityCargoWay cargoWay : list) {
            cargoWay.setDiscount(facilityCargoWay.getDiscount());
        }
        return R.status(iFacilityCargoWayService.updateBatchById(list));
    }

    @GetMapping("/detail")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "详情", notes = "传入facilityCargoWay")
    public R<FacilityCargoWayVO> detail(FacilityCargoWay facilityCargoWay) {
        FacilityCargoWay detail = iFacilityCargoWayService.getOne(Condition.getQueryWrapper(facilityCargoWay));
        return R.data(FacilityCargoWayWrapper.build().entityVO(detail));
    }
}
