package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.LifeCoupon;
import shop.alien.entity.store.LifeGroupBuyMain;
import shop.alien.entity.store.StoreGroupInfo;
import shop.alien.entity.store.StoreHotelGroupRoomInfo;
import shop.alien.entity.store.vo.LifeCouponVo;
import shop.alien.entity.store.vo.LifeGroupBuyMainVo;
import shop.alien.entity.store.vo.LifeTuanGouParamVo;
import shop.alien.mapper.LifeCouponMapper;
import shop.alien.mapper.LifeGroupBuyMainMapper;
import shop.alien.mapper.StoreGroupInfoMapper;
import shop.alien.mapper.StoreHotelGroupRoomInfoMapper;
import shop.alien.store.service.LifeCouponService;
import shop.alien.store.service.LifeGroupPackageService;
import shop.alien.util.common.ListToPage;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 团购
 */
@Api(tags = {"一期-团购"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/groupPackage")
@RequiredArgsConstructor
public class LifeGroupPackageController {

    private final LifeGroupPackageService lifeGroupPackageService;

    private final LifeCouponService lifeCouponService;

    private final LifeCouponMapper lifeCouponMapper;

    private final StoreGroupInfoMapper storeGroupInfoMapper;

    private final StoreHotelGroupRoomInfoMapper storeHotelGroupRoomInfoMapper;

    private final LifeGroupBuyMainMapper lifeGroupBuyMainMapper;

    @ApiOperation("新建团购")
    @PostMapping("/addOrUpdateGroupPackage")
    public R<Boolean> addOrUpdateGroupPackage(@RequestBody LifeTuanGouParamVo lifeTuanGouParamVo) {
        log.info("LifeGroupPackageController.addOrUpdateGroupPackage?lifeTuanGouParamVo={}", lifeTuanGouParamVo.toString());
        // 添加审核锚点
//        WebAudit webAudit = new WebAudit();
//        webAudit.setId(String.valueOf(lifeTuanGouParamVo.getTuangou().getId()));
//        webAudit.setType("4");
//        webAudit.setContent(lifeTuanGouParamVo.getTuangou().getName());
//        Integer i = webAuditService.platformAuditRefundTrack(webAudit);
//        if (!(i > 0)) {
//            return R.fail("失败");
//        }
        if (lifeGroupPackageService.addOrUpdateGroupPackage(lifeTuanGouParamVo) > 0) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("删除团购")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "团购主键id", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/deleteGroupPackage")
    public R<Boolean> deleteGroupPackage(@RequestParam("id") Integer id) {
        log.info("LifeGroupPackageController.deleteGroupPackage?id={}", id);
        if (lifeCouponService.removeById(id)) {
            storeGroupInfoMapper.delete(new LambdaQueryWrapper<StoreGroupInfo>().eq(StoreGroupInfo::getGroupId, id));
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("删除酒店团购")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "酒店团购主键id", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/deleteHotelGroupPackage")
    public R<Boolean> deleteHotelGroupPackage(@RequestParam("id") Integer id) {
        log.info("LifeGroupPackageController.deleteHotelGroupPackage?id={}", id);
        if (lifeCouponService.removeById(id)) {
            storeHotelGroupRoomInfoMapper.delete(new LambdaQueryWrapper<StoreHotelGroupRoomInfo>().eq(StoreHotelGroupRoomInfo::getGroupId, id));
            return R.success("成功");
        }
        return R.fail("失败");
    }

    /**
     * @param storeId
     * @param status  状态：-1.待审核  -2.已驳回  0.待使用(已通过), 1.已核销, 2.已过期, 3.待退款, 4.已退款
     * @return
     */
    @ApiOperation("团购列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "couponCode", value = "券id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "券名称", dataType = "String", paramType = "query")
    })
    @GetMapping("/getGroupPackageList")
    private R<IPage<LifeCouponVo>> getGroupPackageList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam("storeId") String storeId,
            @RequestParam("status") String status,
            @RequestParam("couponCode") String couponCode,
            @RequestParam("name") String name) {
        log.info("LifeGroupPackageController.getGroupPackageList?pageNum={}&pageSize={}&storeId={}&status={}", page, size, storeId, status);
        return R.data(lifeGroupPackageService.getGroupPackageList(page, size, storeId, status, couponCode, name));
    }

    @ApiOperation("酒店团购列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "couponCode", value = "券id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "券名称", dataType = "String", paramType = "query")
    })
    @GetMapping("/getHotelGroupPackageList")
    private R<IPage<LifeCouponVo>> getHotelGroupPackageList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam("storeId") String storeId,
            @RequestParam("status") String status,
            @RequestParam("couponCode") String couponCode,
            @RequestParam("name") String name) {
        log.info("LifeGroupPackageController.getHotelGroupPackageList?pageNum={},pageSize={},storeId={},status={}", storeId, status, page, size);
        return R.data(lifeGroupPackageService.getHotelGroupPackageList(page, size, storeId, status, couponCode, name));
    }

    @ApiOperation("暂停团购")
    @GetMapping("/updateGroupPackageStatus")
    public R<Boolean> updateGroupPackageStatus(@RequestParam("id") String id, @RequestParam("status") Integer status) {
        log.info("LifeCouponController.updateGroupPackageStatus?id={}&status={}", id, status);
        if (lifeGroupPackageService.updateGroupPackageStatus(id, status) > 0) {
            return R.success("修改成功");
        }
        return R.fail("修改失败");
    }

    @ApiOperation("团购详情")
    @GetMapping("/getGroupPackageDetail")
    private R<Map<String, Object>> getGroupPackageDetail(@RequestParam("id") String id, @RequestParam("userId") String userId) {
        log.info("LifeGroupPackageController.getGroupPackageDetail?id={}&userId={}", id, userId);
        return R.data(lifeGroupPackageService.getGroupPackageDetail(id, userId));
    }

    @ApiOperation("酒店团购详情")
    @GetMapping("/getGroupHotelPackageDetail")
    private R<Map<String, Object>> getGroupHotelPackageDetail(@RequestParam("id") String id, @RequestParam("userId") String userId) {
        log.info("LifeGroupPackageController.getGroupHotelPackageDetail?id={}&userId={}", id, userId);
        return R.data(lifeGroupPackageService.getGroupHotelPackageDetail(id, userId));
    }

    @ApiOperation("猜你喜欢")
    @GetMapping("/getRecommendForYou")
    private R<IPage<LifeGroupBuyMainVo>> getRecommendForYou() {
        LambdaQueryWrapper<LifeGroupBuyMainVo> wrapper = new LambdaQueryWrapper<>();
        List<LifeGroupBuyMainVo> lifeGroupBuyMains = lifeGroupBuyMainMapper.getLifeGroupInfo(wrapper);
        // 打乱数据顺序
        Collections.shuffle(lifeGroupBuyMains);
//        Random random = new Random();
        return R.data(ListToPage.setPage(lifeGroupBuyMains, 1, 4));
    }
}
