package com.ruoyi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.annotations.UserLoginToken;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.controller.resp.LevelResp;
import com.ruoyi.enums.user.UserEnums;
import com.ruoyi.frequency.customer.entity.Customer;
import com.ruoyi.frequency.customer.service.CustomerService;
import com.ruoyi.frequency.level.entity.Level;
import com.ruoyi.frequency.level.service.LevelService;
import com.ruoyi.frequency.store.entity.Store;
import com.ruoyi.frequency.store.service.StoreService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.RyController;
import com.ruoyi.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.asn1.cms.ecc.ECCCMSSharedInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;


/**
 * 等级
 */
@CrossOrigin
@RestController
@RequestMapping("/api/level")
@Slf4j
@Api(tags = "等级")
public class ApiLevelController extends RyController {

    @Autowired
    private LevelService levelService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private StoreService storeService;


    @PostMapping("/levelList")
    @ApiOperation(value = "等级列表", notes = "等级列表")
    public ResponseData<List<Level>> levelList(@RequestParam("type") Integer type) {
        List<Level> list = levelService.list(new QueryWrapper<Level>().lambda()
                .eq(Level::getType, type)
                .eq(Level::getDelFlag, PublicCommon.启用).orderByAsc(Level::getCompliance));
        return ResponseData.success(list);
    }

    @PostMapping("/level")
    @ApiOperation(value = "会员等级", notes = "会员等级")
    @UserLoginToken
    public ResponseData<LevelResp> level() {
        LevelResp levelResp = new LevelResp();

        UserVo userVo = getUserVo();


        if (ObjectUtil.equal(UserEnums.customer.getCode(),userVo.getUserType())){
            Customer customer = customerService.getById(userVo.getUserId());

            levelResp.setPrice(customer.getConsumptionAmount());
        }else if (ObjectUtil.equal(UserEnums.store.getCode(),userVo.getUserType())){
            Store store = storeService.getById(userVo.getUserId());

            levelResp.setPrice(store.getIncome());
        }else {
            return ResponseData.success();
        }



        List<Level> levelList = levelService.list(new QueryWrapper<Level>().lambda()
                .eq(Level::getDelFlag, PublicCommon.启用)
                .eq(Level::getType, userVo.getUserType())
                .orderByDesc(Level::getCompliance)
        );
        levelList.stream()
                .filter(s -> levelResp.getPrice().compareTo(s.getCompliance()) >= 0)
                .findFirst()
                .ifPresent(one->{
                    levelResp.setLevel(one.getName());
                    levelResp.setLevelPrice(one.getCompliance());
                    levelResp.setLevelVo(one);
                });

        if (levelResp.getLevel() == null){
            levelResp.setLevelPrice(BigDecimal.ZERO);
            levelResp.setNextLevel(levelList.get(0).getName());
            levelResp.setNextLevelPrice(levelList.get(0).getCompliance());
        }else {
            //排序
            Comparator<Level> comparing = Comparator.comparing(Level::getCompliance);

            levelList.stream().sorted(comparing)
                    .filter(s->levelResp.getPrice().compareTo(s.getCompliance()) < 0)
                    .findFirst()
                    .ifPresent(one->{
                        levelResp.setNextLevel(one.getName());
                        levelResp.setNextLevelPrice(one.getCompliance());
                    });
        }

        return ResponseData.success(levelResp);
    }


}
