package com.izouma.nineth.web.rewardpoints;

import com.izouma.nineth.domain.User;
import com.izouma.nineth.domain.DrawLotteryHistory;
import com.izouma.nineth.domain.PointActivityAward;
import com.izouma.nineth.domain.PointRewardActivity;
import com.izouma.nineth.dto.LotteryHistoryDTO;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.SupplyLotteryDTO;
import com.izouma.nineth.enums.rewardpoints.PointActivityAwardType;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.lock.RedisLockException;
import com.izouma.nineth.service.CollectionService;
import com.izouma.nineth.service.rewardpoints.DrawLotteryHistoryService;
import com.izouma.nineth.service.rewardpoints.PointActivityService;
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.List;

@RestController
@RequestMapping("/pointActivity")
@AllArgsConstructor
public class PointActivityController extends BaseController {
    private final PointActivityService pointActivityService;
    private final DrawLotteryHistoryService drawLotteryHistoryService;
    private final CollectionService collectionService;

    /**
     * 新建积分活动
     * @param input
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public PointRewardActivity save(@RequestBody PointRewardActivity input) {
        return pointActivityService.save(input);
    }

    /**
     * 删除积分活动
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        pointActivityService.softDelete(id);
    }

    /**
     * 查看积分活动详情信息
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/get/{id}")
    public PointRewardActivity get(@PathVariable Long id) {
        return pointActivityService.detail(id);
    }


    /**
     * 更改发奖状态
     * @return 积分活动中奖名单
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/updateReceiveAwards")
    public DrawLotteryHistory updateReceiveAwards(@RequestBody LotteryHistoryDTO input) {
        return drawLotteryHistoryService.updateReceiveAwards(input);
    }

    /**
     * 活动补发
     * @return 积分活动中奖名单
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/supplyAgain")
    public PointActivityAward supplyAgain(@RequestBody SupplyLotteryDTO input) {
        return pointActivityService.supplyAgain(input);
    }



    /**
     * 查看全部积分活动
     * @return
     */
    @PostMapping("/all")
    @PreAuthorize("hasRole('ADMIN')")
    public Page<PointRewardActivity> all(@RequestBody PageQuery pageQuery) {
        return pointActivityService.all(pageQuery);
    }


    /**
     * 查看全部积分活动
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        List<PointRewardActivity> data = pointActivityService.all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    /**
     * 积分活动抽卡
     * @return
     */
    @PostMapping("/draw")
    public PointActivityAward draw(@RequestBody PointRewardActivity pointRewardActivity) {
        User user = currentUser();
        try {
            PointActivityAward pointActivityAward = pointActivityService.drawLottery(pointRewardActivity.getId(), user);
            pointActivityAward.setNum(null);
            pointActivityAward.setStock(null);
            if (PointActivityAwardType.ASSET.equals(pointActivityAward.getType())){
                collectionService.increaseStockCache(pointActivityAward.getAwardCollectionId(), -1);
            }
            return pointActivityAward;
        }catch(RedisLockException e){
            throw new BusinessException("抽取过快，请再试一次");
        }

    }

    /**
     * 当前进行的积分抽奖活动
     * @return
     */
    @PostMapping("/now")
    public PointRewardActivity now() {
        return pointActivityService.now();
    }

    /**
     * 获取当前进行的抽奖活动滚动记录
     * @return
     */
    @PostMapping("/nowActivityAwards")
    public List<DrawLotteryHistory> nowActivityAwards() {
        return pointActivityService.nowActivityAwards();
    }


    /**
     * 我的奖品
     * @return 我本次活动的奖品
     */
    @PostMapping("/awards")
    public Page<DrawLotteryHistory> awards(@RequestBody PageQuery pageQuery) {
        return drawLotteryHistoryService.awards(pageQuery,currentUser().getId());
    }


    /**
     * 查看积分活动中奖名单
     * @return 积分活动中奖名单
     */
    @PostMapping("/activityAwards")
    public Page<DrawLotteryHistory> activityAwards(@RequestBody PageQuery pageQuery) {
        return drawLotteryHistoryService.awards(pageQuery,null);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/excelAwards")
    @ResponseBody
    public void excelAwards(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        List<DrawLotteryHistory> data = drawLotteryHistoryService.awards(pageQuery,null).getContent();
        ExcelUtils.export(response, data);
    }


}

