package com.izouma.nineth.web.rewardpoints;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.domain.UserIntegral;
import com.izouma.nineth.domain.UserIntegralChangeLog;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.enums.IntegralChangeType;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.service.rewardpoints.UserIntegralChangeLogService;
import com.izouma.nineth.utils.excel.ExcelUtils;
import com.izouma.nineth.web.BaseController;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户积分变动记录
 *
 */
@RestController
@RequestMapping("/userIntegralChangeLog")
@AllArgsConstructor
public class UserIntegralChangeLogController extends BaseController {
    private UserIntegralChangeLogService userIntegralChangeLogService;

    @PostMapping("/all")
    public Page<UserIntegralChangeLog> all(@RequestBody PageQuery pageQuery) {
        return userIntegralChangeLogService.all(pageQuery);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        List<UserIntegralChangeLog> data = userIntegralChangeLogService.all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    /**
     * 赠送积分
     * @param reqData
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/presentIntegral")
    @ResponseBody
    public Integer presentIntegral(@RequestBody JSONObject reqData){

        Long integral = reqData.getLong("integral");
        if(integral == null){
            throw new BusinessException("没有指定赠送积分，赠送失败");
        }

        if(integral <= 0){
            throw new BusinessException("赠送积分不能小于0");
        }

        List<Long> userIds = new ArrayList<>();
        Long userId = reqData.getLong("userId");
        JSONArray userIdList = reqData.getJSONArray("userIdList");
        if(userId != null){
            userIds.add(userId);
        }
        if(userIdList != null){
            userIdList.forEach(item -> {
                userIds.add(Long.valueOf(item.toString()));
            });
        }

        Integer count = 0;
        for(Long uId : userIds){
            userIntegralChangeLogService.createByChangeType(IntegralChangeType.PRESENT, uId, integral);
            count++;
        }

        return count;
    }



    /**
     * 积分变更记录
     * @param pageQuery
     * @return
     */
    @PostMapping("changeLogList")
    public Page<UserIntegralChangeLog> changeLogList(@RequestBody  PageQuery pageQuery){
        User user = currentUser();
        pageQuery.getQuery().put("userId", user.getId());
        Page<UserIntegralChangeLog> page = userIntegralChangeLogService.all(pageQuery);
        return page;
    }




}
