/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.groupbuy.multishop.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.groupbuy.comment.api.dto.ApiGroupActivityDto;
import com.yami.shop.groupbuy.comment.model.GroupProd;
import com.yami.shop.groupbuy.comment.model.GroupSku;
import com.yami.shop.groupbuy.comment.service.GroupActivityService;
import com.yami.shop.groupbuy.comment.service.GroupProdService;
import com.yami.shop.groupbuy.comment.service.GroupSkuService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 拼团活动商品规格
 *
 * @author LGH
 * @date 2019-08-27 17:55:57
 */
@RestController
@AllArgsConstructor
@RequestMapping("/group/sku")
public class GroupSkuController {

    private final GroupSkuService groupSkuService;
    private final GroupActivityService groupActivityService;
    private final GroupProdService groupProdService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param groupSku 拼团活动商品规格
     * @return 分页数据
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<GroupSku>> getGroupSkuPage(PageParam<GroupSku> page, GroupSku groupSku) {
        return ResponseEntity.ok(groupSkuService.page(page, new LambdaQueryWrapper<GroupSku>()));
    }


    /**
     * 通过id查询拼团活动商品规格
     *
     * @param groupSkuId id
     * @return 单个数据
     */
    @GetMapping("/info/{groupSkuId}")
    public ResponseEntity<GroupSku> getById(@PathVariable("groupSkuId") Long groupSkuId) {
        return ResponseEntity.ok(groupSkuService.getById(groupSkuId));
    }

    /**
     * 新增拼团活动商品规格
     *
     * @param groupSku 拼团活动商品规格
     * @return 是否新增成功
     */
    @SysLog("新增拼团活动商品规格")
    @PostMapping
    public ResponseEntity<Boolean> save(@RequestBody @Valid GroupSku groupSku) {
        return ResponseEntity.ok(groupSkuService.save(groupSku));
    }

    /**
     * 修改拼团活动商品规格
     *
     * @param groupSku 拼团活动商品规格
     * @return 是否修改成功
     */
    @SysLog("修改拼团活动商品规格")
    @PutMapping
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid GroupSku groupSku) {
        return ResponseEntity.ok(groupSkuService.updateById(groupSku));
    }

    /**
     * 通过id删除拼团活动商品规格
     *
     * @param groupSkuId id
     * @return 是否删除成功
     */
    @SysLog("删除拼团活动商品规格")
    @DeleteMapping("/{groupSkuId}")
    public ResponseEntity<Boolean> removeById(@PathVariable Long groupSkuId) {
        return ResponseEntity.ok(groupSkuService.removeById(groupSkuId));
    }

    /**
     * 批量修改拼团活动商品规格
     */
    @SysLog("批量修改拼团活动商品规格")
    @PutMapping("/updateSkuBatch")
    public ResponseEntity<Boolean> updateSkuBatch(@RequestBody List<GroupSku> groupSkuList) {
        if (CollectionUtil.isEmpty(groupSkuList)) {
            return ResponseEntity.ok().build();
        }
        GroupProd groupProd = groupProdService.getById(groupSkuList.get(0).getGroupProdId());
        ApiGroupActivityDto apiGroupActivityInfo = groupActivityService.getApiGroupActivityInfo(groupProd.getGroupActivityId(), groupProd.getProdId());
        if (apiGroupActivityInfo.getHasLeaderPrice() == 0) {
            groupSkuList.stream().forEach(groupSku -> groupSku.setLeaderPrice(groupSku.getActPrice()));
        }
        return ResponseEntity.ok(groupSkuService.updateBatchById(groupSkuList));
    }
}
