/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.user.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.user.comment.model.UserRights;
import com.yami.shop.user.comment.service.UserRightsService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.AbstractDocument;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;


/**
 * 用户权益信息管理实现
 *
 * @author LGH
 * @version 1.0.0
 * @since 2020-02-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user/userRights" )
public class UserRightsController {

    /**
     * 系统核销中最大的权益id
     */
    private final Integer MAX_RIGHTSID_OF_SERVICETYPE = 5;
    private final UserRightsService userRightsService;

    /**
     * 权益列表
     */
    @GetMapping
    public ResponseEntity<IPage<UserRights>> listUserLevel(PageParam<UserRights> page, UserRights userRights) {
        return ResponseEntity.ok( userRightsService.page(page, new LambdaQueryWrapper<UserRights>()
                .like(!StrUtil.isBlank(userRights.getRightsName()),UserRights::getRightsName, userRights.getRightsName())
                .eq(UserRights::getStatus, 1)
                .eq(!Objects.isNull(userRights.getServiceType()),UserRights::getServiceType, userRights.getServiceType())
                .orderByDesc(UserRights::getServiceType)
                .orderByAsc(UserRights::getSeq)
        ));
    }

    /**
     * 通过id查询
     * @param rightsId id
     * @return 单个数据
     */
    @GetMapping("/info/{rightsId}" )
    public ResponseEntity<UserRights> getById(@PathVariable("rightsId") Long rightsId) {
        return ResponseEntity.ok(userRightsService.getById(rightsId));
    }

    /**
     * 新增
     * @param userRights
     * @return 是否新增成功
     */
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user:userRights:save')" )
    public ResponseEntity<Boolean> save(@RequestBody @Valid UserRights userRights) {
        // 1:商家线下核销
        userRights.setServiceType(1);
        userRights.setStatus(1);
        return ResponseEntity.ok(userRightsService.save(userRights));
    }

    /**
     * 修改
     * @param userRights
     * @return 是否修改成功
     */
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user:userRights:update')" )
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid UserRights userRights) {
        return ResponseEntity.ok(userRightsService.updateById(userRights));
    }

    /**
     * 通过id删除
     * @param rightsId
     * @return 是否删除成功
     */
    @SysLog("删除" )
    @DeleteMapping("/{rightsId}" )
    @PreAuthorize("@pms.hasPermission('user:userRights:delete')" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long rightsId) {
//        Boolean isSuccess = userRightsService.removeRights(rightsId);
        return ResponseEntity.ok(userRightsService.removeRights(rightsId));
    }

//    /**
//     * 启用系统核销权益
//     * @param rightsIds
//     */
//    @SysLog("启用系统核销权益" )
//    @PutMapping("/setStatus")
//    @PreAuthorize("@pms.hasPermission('user:userRights:update')" )
//    public ResponseEntity<Void> updateById(@RequestBody @Valid Long[] rightsIds) {
//        for (Long rightsId:rightsIds){
//            userRightsService.setStatua(rightsId,1);
//        }
//        return ResponseEntity.ok().build();
//    }
//    /**
//     * 获取系统核销列表
//     */
//    @GetMapping("/getList")
//    public ResponseEntity<List<UserRights>> getList() {
//        return ResponseEntity.ok( userRightsService.list(new LambdaQueryWrapper<UserRights>()
//                .eq(UserRights::getServiceType, 0)
//                .orderByAsc(UserRights::getSeq)
//        ));
//    }
    /**
     * 获取权益列表
     */
    @GetMapping("/list")
    public ResponseEntity<List<UserRights>> list() {
        return ResponseEntity.ok( userRightsService.list(new LambdaQueryWrapper<UserRights>()
                .eq(UserRights::getServiceType, 1)
                .orderByAsc(UserRights::getSeq)
        ));
    }


    /**
     * 通过id查询
     * @return 单个数据
     */
    @GetMapping("/getMaxSeq" )
    public ResponseEntity<Integer> getMaxSeq() {
        List<UserRights> list = userRightsService.list(new LambdaQueryWrapper<UserRights>().eq(UserRights::getServiceType, Constant.SERVICE_TYPE));
        Integer seq = 1;
        for (UserRights userRights:list){
            if (userRights.getSeq()> seq){
                seq = userRights.getSeq();
            }
        }
        seq++;
        return ResponseEntity.ok(seq);
    }
}
