package com.gxwebsoft.love.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.love.entity.UserPlan;
import com.gxwebsoft.love.entity.UserPlanIcon;
import com.gxwebsoft.love.entity.UserPlanPrice;
import com.gxwebsoft.love.param.UserPlanParam;
import com.gxwebsoft.love.service.UserPlanIconService;
import com.gxwebsoft.love.service.UserPlanPriceService;
import com.gxwebsoft.love.service.UserPlanService;
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.List;

/**
 * 会员套餐管理表控制器
 *
 * @author 科技小王子
 * @since 2023-06-21 11:21:01
 */
@Api(tags = "会员套餐管理表管理")
@RestController
@RequestMapping("/api/love/user-plan")
public class UserPlanController extends BaseController {
    @Resource
    private UserPlanService userPlanService;
    @Resource
    private UserPlanPriceService userPlanPriceService;
    @Resource
    private UserPlanIconService userPlanIconService;

    @PreAuthorize("hasAuthority('love:userPlan:list')")
    @OperationLog
    @ApiOperation("分页查询会员套餐管理表")
    @GetMapping("/page")
    public ApiResult<PageResult<UserPlan>> page(UserPlanParam param) {
        // 使用关联查询
        return success(userPlanService.pageRel(param));
    }

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

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

    @PreAuthorize("hasAuthority('love:userPlan:save')")
    @OperationLog
    @ApiOperation("添加会员套餐管理表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody UserPlan userPlan) {
        if (userPlanService.save(userPlan)) {
            if (!userPlan.getPrice().isEmpty()) {
              final List<UserPlanPrice> price = userPlan.getPrice();
              price.forEach(d -> {
                d.setPlanId(userPlan.getPlanId());
              });
              userPlanPriceService.saveBatch(price);
            }
            if (!userPlan.getIcons().isEmpty()) {
              final List<UserPlanIcon> icons = userPlan.getIcons();
              icons.forEach(d -> {
                d.setPlanId(userPlan.getPlanId());
              });
              userPlanIconService.saveBatch(icons);
            }
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('love:userPlan:update')")
    @OperationLog
    @ApiOperation("修改会员套餐管理表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserPlan userPlan) {
        if (userPlanService.updateById(userPlan)) {
            // 先删除
            userPlanPriceService.remove(new LambdaQueryWrapper<UserPlanPrice>().eq(UserPlanPrice::getPlanId, userPlan.getPlanId()));
            userPlanIconService.remove(new LambdaQueryWrapper<UserPlanIcon>().eq(UserPlanIcon::getPlanId, userPlan.getPlanId()));
            // 再添加
            if (!userPlan.getPrice().isEmpty()) {
              final List<UserPlanPrice> price = userPlan.getPrice();
              price.forEach(d -> {
                d.setPlanId(userPlan.getPlanId());
              });
              userPlanPriceService.saveBatch(price);
            }
            if (!userPlan.getIcons().isEmpty()) {
              final List<UserPlanIcon> icons = userPlan.getIcons();
              icons.forEach(d -> {
                d.setPlanId(userPlan.getPlanId());
              });
              userPlanIconService.saveBatch(icons);
            }
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
