package com.ruoyi.hywz.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.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.hywz.entity.AdsMatHyxtInvoiceUserApplyPermissionD;
import com.ruoyi.hywz.entity.vo.UserPermissionVo;
import com.ruoyi.hywz.service.IAdsMatHyxtInvoiceUserApplyPermissionDService;
import com.ruoyi.hywz.utils.IdGen;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * @author: jxh
 * @description:
 * @create: 2024/4/16
 */
@Api(value = "userApplyPermissionD", tags = "用户申请权限")
@RestController
@AllArgsConstructor
@RequestMapping("userApplyPermissionD")
public class UserApplyPermissionDController extends BaseController {

    private final IAdsMatHyxtInvoiceUserApplyPermissionDService adsMatHyxtInvoiceUserApplyPermissionDService;

    @ApiOperation("分页查询")
    @PostMapping("/page")
    public R<IPage<UserPermissionVo>> page(@RequestParam(defaultValue = "10") Long pageSize,
                                           @RequestParam(defaultValue = "1") Long pageNo,
                                           @RequestBody(required = false) UserPermissionVo param) {
        Page<UserPermissionVo> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        QueryWrapper<UserPermissionVo> queryWrapper = Wrappers.query();
        if (ObjectUtils.isNotEmpty(param)) {
            queryWrapper.like(StringUtils.isNotEmpty(param.getUserName()), "a.user_name", param.getUserName());
            queryWrapper.like(StringUtils.isNotEmpty(param.getNickName()), "a.nick_name", param.getNickName());
            queryWrapper.like(StringUtils.isNotEmpty(param.getPhonenumber()), "a.phonenumber", param.getPhonenumber());
            if (ObjectUtils.isNotEmpty(param.getApplyPermission())) {
                if (param.getApplyPermission() == 0) {
                    queryWrapper.isNull("b.apply_permission");
                }
                if (param.getApplyPermission() == 1) {
                    queryWrapper.eq("b.apply_permission", 1);
                }
            }
        }

        queryWrapper.orderByDesc("b.apply_permission");
        return R.ok(adsMatHyxtInvoiceUserApplyPermissionDService.queryPage(page, queryWrapper));
    }

    @ApiOperation("修改权限")
    @PostMapping("/updateApplyPermission")
    public R updateApplyPermission(@RequestBody UserPermissionVo param) {
        if (ObjectUtils.isEmpty(param.getUserId())) {
            return R.fail("参数：userId不能为空！");
        }
        if (ObjectUtils.isEmpty(param.getApplyPermission())) {
            return R.fail("参数：applyPermission不能为空！");
        }
        Integer applyPermission = param.getApplyPermission();
        if (!Arrays.asList(0,1).contains(applyPermission)) {
            return R.fail("参数：applyPermission的值只能是0和1");
        }

        // 1.先删除
        LambdaQueryWrapper<AdsMatHyxtInvoiceUserApplyPermissionD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtInvoiceUserApplyPermissionD::getUserId, param.getUserId());
        adsMatHyxtInvoiceUserApplyPermissionDService.remove(queryWrapper);

        // 2.如果开启则新增
        if (applyPermission == 1) {
            Date date = new Date();
            AdsMatHyxtInvoiceUserApplyPermissionD model = new AdsMatHyxtInvoiceUserApplyPermissionD();
            model.setId(IdGen.get32UUID());
            model.setUserId(param.getUserId());
            model.setCreateTime(date);
            model.setIsDeleted(0);
            adsMatHyxtInvoiceUserApplyPermissionDService.save(model);
        }
        return R.ok();
    }


}
