/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.groupon.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
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.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.core.tool.utils.Func;
import org.springblade.modules.groupon.entity.SelfTake;
import org.springblade.modules.groupon.entity.UserSelfTake;
import org.springblade.modules.groupon.param.AddUserSelfTakeParam;
import org.springblade.modules.groupon.service.ISelfTakeService;
import org.springblade.modules.groupon.service.IUserSelfTakeService;
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 javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自提点表 控制器
 *
 * @author BladeX
 * @since 2023-06-02
 */
@RestController
@AllArgsConstructor
@RequestMapping("groupon/selftake")
@Api(value = "自提点表", tags = "自提点表接口")
public class SelfTakeController extends BladeController {

    private final ISelfTakeService selfTakeService;
    @Resource
    private IUserSelfTakeService userSelfTakeService;

    /**
     * 查询自己的自提点
     */
    @PostMapping("/listByUserId")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询团长自提点", notes = "userId")
    public R listByUserId(@RequestParam Long userId) {

        List<UserSelfTake> list = userSelfTakeService.lambdaQuery()
                .eq(UserSelfTake::getUserId, userId)
                .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::getStatus, 1);
        q.eq(SelfTake::getIsDelete, 0);
        q.orderByDesc(SelfTake::getIsDefault);
        q.orderByDesc(SelfTake::getCreateTime);
		List<SelfTake> list1 = selfTakeService.list(q);
		return R.data(list1);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入selfTake")
    public R<SelfTakeVO> detail(SelfTake selfTake) {
        SelfTake detail = selfTakeService.getOne(Condition.getQueryWrapper(selfTake));
        return R.data(SelfTakeWrapper.build().entityVO(detail));
    }

    /**
     * 分页 自提点表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入selfTake")
    public R<IPage<SelfTakeVO>> list(SelfTake selfTake, Query query, String isAdmin) {
        String name = "";
        if (StrUtil.isNotBlank(selfTake.getName())) {
            name = selfTake.getName();
            selfTake.setName(null);
        }

        QueryWrapper<SelfTake> queryWrapper = Condition.getQueryWrapper(selfTake);
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        if (StrUtil.isBlank(isAdmin)) {
            queryWrapper.isNotNull("contact_number").ne("contact_number", "");
        }
        IPage<SelfTake> pages = selfTakeService.page(Condition.getPage(query), queryWrapper);
        return R.data(SelfTakeWrapper.build().pageVO(pages));
    }

    /**
     * 自定义分页 自提点表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入selfTake")
    public R<IPage<SelfTakeVO>> page(SelfTakeVO selfTake, Query query) {
        // 处理前端下拉多选时的bug
        if (NumberUtil.isNumber(selfTake.getName()) && selfTake.getName().length() == 19) {
            selfTake.setName("");
        }
        IPage<SelfTakeVO> pages = selfTakeService.selectSelfTakePage(Condition.getPage(query), selfTake);
        return R.data(pages);
    }

    /**
     * 新增 自提点表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入selfTake")
    public R save(@Valid @RequestBody SelfTake selfTake) {
        return R.status(selfTakeService.save(selfTake));
    }

    /**
     * 修改 自提点表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入selfTake")
    public R update(@Valid @RequestBody SelfTake selfTake) {
        return R.status(selfTakeService.updateById(selfTake));
    }

    /**
     * 新增或修改 自提点表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入selfTake")
    public R submit(@Valid @RequestBody SelfTake selfTake) {
        return R.status(selfTakeService.saveOrUpdate(selfTake));
    }

    /**
     * 删除 自提点表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(selfTakeService.removeByIds(Func.toLongList(ids)));
    }

    /**
     * 给团长添加自提点
     */
    @PostMapping("/addUserSelfTake")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "给团长添加自提点", notes = "传入ids")
    public R addUserSelfTake(@RequestBody AddUserSelfTakeParam param) {
        userSelfTakeService.lambdaUpdate()
                .eq(UserSelfTake::getUserId, param.getUserId())
                .remove();
        List<UserSelfTake> list = new ArrayList<>();
        for (Long selfTakeId : param.getSelfTakeList()) {
            UserSelfTake userSelfTake = new UserSelfTake();
            userSelfTake.setUserId(param.getUserId());
            userSelfTake.setSelfTakeId(selfTakeId);
            list.add(userSelfTake);
        }
        userSelfTakeService.saveBatch(list);
        return R.success("操作成功");
    }


}
