package org.springblade.modules.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.constant.CommonConstant;
import org.springblade.core.boot.ctrl.BladeController;
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.groupon.entity.SelfTake;
import org.springblade.modules.groupon.entity.UserSelfTake;
import org.springblade.modules.groupon.service.ISelfTakeService;
import org.springblade.modules.groupon.service.IUserSelfTakeService;
import org.springblade.modules.groupon.service.impl.SelfTakeServiceImpl;
import org.springblade.modules.groupon.vo.SelfTakeVO;
import org.springblade.modules.groupon.wrapper.SelfTakeWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 团购表 控制器
 *
 * @author BladeX
 * @since 2023-06-02
 */
@RestController
@AllArgsConstructor
@RequestMapping(CommonConstant.API_URL)
@Api(value = "自提点接口", tags = "自提点接口")
public class ApiSelfTakeController extends BladeController {

    @Resource
    private ISelfTakeService selfTakeService;

    @Resource
    private IUserSelfTakeService userSelfTakeService;

    @ApiOperation(value = "分页查询所有自提点", notes = "传入selfTake")
    @GetMapping("selfTake/all")
    public R<IPage<SelfTakeVO>> getAllSelfTake(SelfTake selfTake, Query query) {
        QueryWrapper<SelfTake> queryWrapper = Condition.getQueryWrapper(selfTake);
        queryWrapper.isNotNull("contact_number").ne("contact_number", "");
        IPage<SelfTake> pages = selfTakeService.page(Condition.getPage(query), queryWrapper);
        return R.data(SelfTakeWrapper.build().pageVO(pages));
    }

    /**
     * 分页查询自己的自提点
     */
    @PostMapping("selfTake/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页", notes = "传入selfTake")
    public R<IPage<SelfTakeVO>> list(Query query) {

        List<UserSelfTake> list = userSelfTakeService.lambdaQuery()
                .eq(UserSelfTake::getUserId, getUser().getUserId())
                .list();
        if (list.size()<=0){
            return R.success("没有查询到自提点");
        }

        List<Long> collect = list.stream().map(UserSelfTake::getSelfTakeId).collect(Collectors.toList());

        LambdaQueryWrapper<SelfTake> q = new LambdaQueryWrapper<>();
        q.in(SelfTake::getId,collect);
        q.eq(SelfTake::getIsDelete,0);
        q.orderByDesc(SelfTake::getIsDefault);
        q.orderByDesc(SelfTake::getCreateTime);
        IPage<SelfTake> pages = selfTakeService.page(Condition.getPage(query), q);
        return R.data(SelfTakeWrapper.build().pageVO(pages));
    }

    /**
     * 修改自己的自提点
     */
    @PostMapping("selfTake/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改", notes = "传入selfTake")
    public R update(SelfTake selfTake) {
        selfTakeService.updateById(selfTake);
        return R.success("操作成功！");
    }

    /**
     * 删除自己的自提点
     */
    @PostMapping("selfTake/delete")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除", notes = "自提点id")
    public R delete(@ApiParam(value = "自提点id", required = true) @RequestParam Long id){
        selfTakeService.lambdaUpdate()
                .set(SelfTake::getIsDelete,1)
                .eq(SelfTake::getCreateTime,getUser().getUserId())
                .eq(SelfTake::getId,id)
                .update();
        return R.success("操作成功！");
    }

    /**
     * 设为默认自提点
     */
    @PostMapping("selfTake/default")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "设为默认自提点", notes = "自提点id")
    public R defaultSelfTake(@ApiParam(value = "自提点id", required = true) @RequestParam Long id) {
        SelfTake selfTake = selfTakeService.lambdaQuery()
                .eq(SelfTake::getCreateUserId,getUser().getUserId())
                .eq(SelfTake::getId,id)
                .eq(SelfTake::getIsDelete,0)
                .one();
        if (Objects.isNull(selfTake)){
            return R.fail("没有查询到该自提点");
        }
        selfTakeService.lambdaUpdate().set(SelfTake::getIsDefault, 0)
                .eq(SelfTake::getCreateUserId, getUser().getUserId())
                .update();
        selfTake.setIsDefault(1);
        selfTakeService.updateById(selfTake);
        return R.success("操作成功！");
    }



}
