package com.gxwebsoft.love.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.love.entity.UserPlanEquity;
import com.gxwebsoft.love.param.UserPlanEquityParam;
import com.gxwebsoft.love.service.UserPlanEquityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 会员套餐管理表控制器
 *
 * @author 科技小王子
 * @since 2023-06-23 11:34:03
 */
@Api(tags = "会员套餐管理表管理")
@RestController
@RequestMapping("/api/love/user-plan-equity")
public class UserPlanEquityController extends BaseController {
    @Resource
    private UserPlanEquityService userPlanEquityService;

    @PreAuthorize("hasAuthority('love:userPlanEquity:list')")
    @OperationLog
    @ApiOperation("分页查询会员套餐管理表")
    @GetMapping("/page")
    public ApiResult<PageResult<UserPlanEquity>> page(UserPlanEquityParam param) {
        final PageResult<UserPlanEquity> result = userPlanEquityService.pageRel(param);
        // 门店身份权益
        if (param.getType().equals(1)) {
          final Map<Integer, List<UserPlanEquity>> collect = result.getList().stream().collect(Collectors.groupingBy(UserPlanEquity::getUserId));
          result.setList(null);
          final ArrayList<UserPlanEquity> list = new ArrayList<>();
          for(Integer userId : collect.keySet()){
            UserPlanEquity equity = new UserPlanEquity();
            final List<UserPlanEquity> userPlanEquities = collect.get(userId);
            list.add(userPlanEquities.get(0));
          }
          result.setList(list);
          return success(result);
        }
        // 使用关联查询
        return success(result);
    }

    @PreAuthorize("hasAuthority('love:userPlanEquity:list')")
    @OperationLog
    @ApiOperation("查询全部会员套餐管理表")
    @GetMapping()
    public ApiResult<List<UserPlanEquity>> list(UserPlanEquityParam param) {
        // 使用关联查询
        return success(userPlanEquityService.listRel(param));
    }

    @PreAuthorize("hasAuthority('love:userPlanEquity:list')")
    @OperationLog
    @ApiOperation("根据id查询会员套餐管理表")
    @GetMapping("/{id}")
    public ApiResult<UserPlanEquity> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(userPlanEquityService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('love:userPlanEquity:save')")
    @OperationLog
    @ApiOperation("添加会员套餐管理表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody UserPlanEquity userPlanEquity) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          userPlanEquity.setUserId(loginUser.getUserId());
        }
        if (userPlanEquityService.save(userPlanEquity)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanEquity:update')")
    @OperationLog
    @ApiOperation("修改会员套餐管理表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserPlanEquity userPlanEquity) {
        if (userPlanEquityService.updateById(userPlanEquity)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanEquity:remove')")
    @OperationLog
    @ApiOperation("删除会员套餐管理表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (userPlanEquityService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanEquity:save')")
    @OperationLog
    @ApiOperation("批量添加会员套餐管理表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<UserPlanEquity> list) {
        if (userPlanEquityService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanEquity:update')")
    @OperationLog
    @ApiOperation("批量修改会员套餐管理表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<UserPlanEquity> batchParam) {
        if (batchParam.update(userPlanEquityService, "plan_equity_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('love:userPlanEquity:remove')")
    @OperationLog
    @ApiOperation("批量删除会员套餐管理表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (userPlanEquityService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
