package com.gxwebsoft.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.App;
import com.gxwebsoft.oa.entity.AppRenew;
import com.gxwebsoft.oa.param.AppRenewParam;
import com.gxwebsoft.oa.service.AppRenewService;
import com.gxwebsoft.oa.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 续费管理控制器
 *
 * @author 科技小王子
 * @since 2023-11-14 14:27:57
 */
@Api(tags = "续费管理管理")
@RestController
@RequestMapping("/api/oa/app-renew")
public class AppRenewController extends BaseController {
    @Resource
    private AppRenewService appRenewService;
    @Resource
    private AppService appService;

    @ApiOperation("分页查询续费管理")
    @GetMapping("/page")
    public ApiResult<PageResult<AppRenew>> page(AppRenewParam param) {
        // 使用关联查询
        return success(appRenewService.pageRel(param));
    }

    @ApiOperation("查询全部续费管理")
    @GetMapping()
    public ApiResult<List<AppRenew>> list(AppRenewParam param) {
        // 使用关联查询
        return success(appRenewService.listRel(param));
    }

    @ApiOperation("根据id查询续费管理")
    @GetMapping("/{id}")
    public ApiResult<AppRenew> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(appRenewService.getByIdRel(id));
    }

    @ApiOperation("添加续费管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody AppRenew appRenew) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          appRenew.setUserId(loginUser.getUserId());
        }
        if (appRenewService.save(appRenew)) {
            // 更新到期时间
            App app = appService.getById(appRenew.getAppId());
            app.setExpirationTime(appRenew.getEndTime());
            app.setRenewMoney(appRenew.getMoney());
            app.setShowExpiration(true);
            appService.updateById(app);
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改续费管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AppRenew appRenew) {
        if (appRenewService.updateById(appRenew)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除续费管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (appRenewService.removeById(id)) {
          // 没有续费单则不显示在续费提醒列表
          AppRenew appRenew = appRenewService.getById(id);
          if (appRenewService.count(new LambdaQueryWrapper<AppRenew>().eq(AppRenew::getAppId,appRenew.getAppId())) == 0) {
            App app = new App();
            app.setAppId(appRenew.getAppId());
            app.setShowExpiration(false);
            appService.updateById(app);
          }
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加续费管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<AppRenew> list) {
        if (appRenewService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改续费管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<AppRenew> batchParam) {
        if (batchParam.update(appRenewService, "app_renew_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除续费管理")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (appRenewService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
