
package com.tmerclub.cloud.user.controller.platform;

import com.tmerclub.cloud.api.coupon.feign.CouponFeignClient;
import com.tmerclub.cloud.api.coupon.vo.CouponDataVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.user.constant.LevelTypeEnum;
import com.tmerclub.cloud.user.dto.UserRightsDTO;
import com.tmerclub.cloud.user.service.UserLevelService;
import com.tmerclub.cloud.user.service.UserRightsService;
import com.tmerclub.cloud.user.vo.UserRightsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户权益信息
 *
 * @author YXF
 * @date 2020-12-08 10:16:53
 */
@RestController("platformUserRightsController")
@RequestMapping("/p/userRights")
@Tag(name = "店铺-用户权益信息")
public class UserRightsController {

    @Autowired
    private UserRightsService userRightsService;
    @DubboReference
    private CouponFeignClient couponFeignClient;
    @Autowired
    private UserLevelService userLevelService;

    @GetMapping("/page")
    @Operation(summary = "获取用户权益信息列表", description = "分页获取用户权益信息列表")
    public ServerResponseEntity<PageVO<UserRightsVO>> page(@Valid PageDTO pageDTO, UserRightsDTO userRightsDTO) {
        PageVO<UserRightsVO> userRightsPage = userRightsService.page(pageDTO, userRightsDTO);
        return ServerResponseEntity.success(userRightsPage);
    }


    @GetMapping
    @Operation(summary = "获取用户权益信息", description = "根据rightsId获取用户权益信息")
    public ServerResponseEntity<UserRightsVO> getByRightsId(@RequestParam Long rightsId) {
        UserRightsVO userRightsVO = userRightsService.getByRightsId(rightsId);
        ServerResponseEntity<List<CouponDataVO>> couponListResponse = couponFeignClient.getCouponListByCouponIds(userRightsVO.getCouponIds());
        userRightsVO.setCouponList(couponListResponse.getData());
        return ServerResponseEntity.success(userRightsVO);
    }

    @PostMapping
    @Operation(summary = "保存用户权益信息", description = "保存用户权益信息")
    public ServerResponseEntity<Long> save(@Valid @RequestBody UserRightsDTO userRightsDTO) {
        userRightsService.save(userRightsDTO);
        return ServerResponseEntity.success(userRightsDTO.getRightsId());
    }

    @PutMapping
    @Operation(summary = "更新用户权益信息", description = "更新用户权益信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserRightsDTO userRightsDTO) {
        userRightsService.update(userRightsDTO);
        userRightsService.removeRightsCache(userRightsDTO.getRightsId());
        userRightsService.removeRightsByLevelTypeCache(LevelTypeEnum.ORDINARY_USER.value());
        userRightsService.removeRightsByLevelTypeCache(LevelTypeEnum.PAY_USER.value());
        // 清楚会员等级缓存
        userLevelService.removeLevelListCache(0);
        userLevelService.removeLevelListCache(1);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除用户权益信息", description = "根据用户权益信息id删除用户权益信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long rightsId) {
        userRightsService.deleteById(rightsId);
        userRightsService.removeRightsCache(rightsId);
        userRightsService.removeRightsByLevelTypeCache(LevelTypeEnum.ORDINARY_USER.value());
        userRightsService.removeRightsByLevelTypeCache(LevelTypeEnum.PAY_USER.value());
        // 清楚会员等级缓存
        userLevelService.removeLevelListCache(0);
        userLevelService.removeLevelListCache(1);
        return ServerResponseEntity.success();
    }

    /**
     * 获取权益列表
     */
    @GetMapping("/list")
    public ServerResponseEntity<List<UserRightsVO>> list() {
        UserRightsDTO userRights = new UserRightsDTO();
        return ServerResponseEntity.success(userRightsService.list(userRights));
    }
}
