package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeGroupBuyThali;
import shop.alien.entity.store.dto.LifeGroupBuyDto;
import shop.alien.entity.store.vo.LifeGroupBuyThaliVo;
import shop.alien.store.service.LifeGroupBuyService;

/**
 * 团购
 */
@Api(tags = {"重构-团购"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/groupBuy")
@RequiredArgsConstructor
public class LifeGroupBuyController {

    private final LifeGroupBuyService lifeGroupBuyService;

    @ApiOperation("保存套餐团购")
    @PostMapping("/saveThali")
    public R saveThali(@RequestBody LifeGroupBuyDto lifeGroupBuyDto) {
        log.info("LifeGroupBuyController.saveThali?lifeGroupBuyDto={}", lifeGroupBuyDto.toString());
        if (lifeGroupBuyService.saveThali(lifeGroupBuyDto)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("保存套餐团购（返回保存套餐）")
    @PostMapping("/saveThaliNew")
    public R<LifeGroupBuyThali> saveThaliNew(@RequestBody LifeGroupBuyDto lifeGroupBuyDto) {
        log.info("LifeGroupBuyController.saveThaliNew?lifeGroupBuyDto={}", lifeGroupBuyDto.toString());
        LifeGroupBuyThali lifeGroupBuyThali = lifeGroupBuyService.saveThaliNew(lifeGroupBuyDto);
        return R.data(lifeGroupBuyThali);
    }

    @ApiOperation("套餐团购列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态(0草稿/1待审核/2未开始/3审核拒绝/4已售罄/5进行中/6已下架/7已结束/8=2+手动下架)", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "groupName", value = "名称", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "groupType", value = "团购类型", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/getThaliList")
    private R<IPage<LifeGroupBuyThaliVo>> getThaliList(@RequestParam(value = "page", defaultValue = "1") int page,
                                                       @RequestParam(value = "size", defaultValue = "10") int size,
                                                       @RequestParam(value = "storeId") String storeId,
                                                       @RequestParam(value = "status", required = false) String status,
                                                       @RequestParam(value = "groupName", required = false) String groupName,
                                                       @RequestParam(value = "groupType", required = false) String groupType) {
        log.info("LifeGroupBuyController.getThaliList?page={},size={},storeId={},status={},groupName={},groupType={}", page, size, storeId, status, groupName, groupType);
        return R.data(lifeGroupBuyService.getThaliList(page, size, storeId, status, groupName, groupType));
    }

    @ApiOperation("id查询套餐团购")
    @GetMapping("/getThaliById")
    private R getThaliById(@RequestParam("id") String id, String userId) {
        log.info("LifeGroupBuyController.getThaliById?id={}&userId={}", id, userId);
        return R.data(lifeGroupBuyService.getThaliById(id, userId));
    }

    @ApiOperation("修改团购状态")
    @GetMapping("/updateStatus")
    private R updateStatus(@RequestParam("id") String id, @RequestParam("status") String status, @RequestParam("approvalComments") String approvalComments) {
        log.info("LifeGroupBuyController.updateStatus?id={},status={},approvalComments={}", id, status, approvalComments);
        if (lifeGroupBuyService.updateStatus(id, status, approvalComments)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("修改团购库存")
    @GetMapping("/updateNum")
    private R updateNum(@RequestParam("id") String id, @RequestParam("num") String num) {
        log.info("LifeGroupBuyController.updateNum?id={},num={}", id, num);
        if (lifeGroupBuyService.updateNum(id, num)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("删除套餐团购")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "groupType", value = "团购类型(1美食)", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/delThaliById")
    private R delThaliById(@RequestParam("id") String id, @RequestParam("groupType") String groupType) {
        log.info("LifeGroupBuyController.delThaliById?id={},groupType={}", id, groupType);
        if (lifeGroupBuyService.delThaliById(id, groupType)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }
}
