package com.zd.exam.controller;

import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.exam.domain.ElPassMarkconfig;
import com.zd.exam.domain.ElPointsLoginfo;
import com.zd.exam.domain.ElPointsRecord;
import com.zd.exam.domain.dto.ElPointsRecordDTO;
import com.zd.exam.domain.vo.ElPointsCountVO;
import com.zd.exam.domain.vo.ElPointsLoginfoVO;
import com.zd.exam.domain.vo.ElPointsRecordListVO;
import com.zd.exam.domain.vo.ElUserPointsLoginfoVO;
import com.zd.exam.service.IElPointsRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhoupan
 * @Date: 2021/09/07/13:53
 * @Description:
 */
@RestController
@RequestMapping("/points/record")
@Api(tags = {"积分记录"})
public class ElPointsRecordController extends BaseController {

    @Autowired
    private IElPointsRecordService pointsRecordService;


    /**
     * 查询积分记录列表  "points:record:list"
     */
    @PreAuthorize(hasPermi = PerPrefix.POINTS_RECORD+ PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(ElPointsRecordDTO pointsRecordDTO) {
        startPage();
        List<ElPointsRecordListVO> list = pointsRecordService.selectElPointsRecordVOList(pointsRecordDTO);
        return getDataTable(list);
    }

    /**
     * 查询我的积分记录列表
     */
    // @PreAuthorize(hasPermi = "points:record:mylist")
    @GetMapping("/list/my")
    public TableDataInfo myList(ElPointsRecordDTO pointsRecordDTO) {
        startPage();
        pointsRecordDTO.setJoinUserId(SecurityUtils.getUserId());
        List<ElPointsRecordListVO> list = pointsRecordService.selectElPointsRecordVOList(pointsRecordDTO);
        return getDataTable(list);
    }



    /**
     * 小程序查询我的积分记录列表
     */
    // @PreAuthorize(hasPermi = "points:record:mylist")
    @RequestMapping("/list/myApp")
    public TableDataInfo myApp(@RequestBody ElPointsRecordDTO pointsRecordDTO) {
        startPage(pointsRecordDTO.getAppPageNum(),pointsRecordDTO.getAppPageSize());
        pointsRecordDTO.setJoinUserId(SecurityUtils.getUserId());
        List<ElPointsRecordListVO> list = pointsRecordService.selectElPointsRecordVOList(pointsRecordDTO);
        return getDataTable(list);
    }

    /**
     * 小程序查询我的积分记录统计
     */
    // @PreAuthorize(hasPermi = "points:record:mylist")
    @RequestMapping("/count/myApp")
    public AjaxResult countMyApp() {
        ElPointsRecordDTO pointsRecordDTO = new ElPointsRecordDTO();
        pointsRecordDTO.setJoinUserId(SecurityUtils.getUserId());
        Integer count = pointsRecordService.selectElCountPointsRecord(pointsRecordDTO);
        return AjaxResult.success(count==null?0:count);
    }

    /**
     * 初始化信用积分，用于证书获取后，给予一个基础分值。
     */
    @ApiOperation(value = "初始化信用积分，用于证书获取后，给予一个基础分值。")
    @Log(title = "初始化信用积分，用于证书获取后，给予一个基础分值。", businessType = BusinessType.INSERT)
    @PostMapping("/initCreditPoints")
    public AjaxResult initCreditPoints(@RequestBody Map<String,Object> initMap)
    {
        return toAjax(pointsRecordService.initCreditPoints(initMap));
    }

    /**
     * 根据用户id，修改积分内容
     */
    @ApiOperation(value = "根据用户id，修改积分内容")
    @Log(title = "根据用户id，修改积分内容", businessType = BusinessType.UPDATE)
    @RequestMapping("/recoveryPoints")
    public AjaxResult recoveryPoints(@RequestBody Map<String,Object> recoveryMap)
    {
        return toAjax(pointsRecordService.recoveryPoints(recoveryMap));
    }

    /**
     * 根据积分类型，扣除积分相关内容
     */
    @ApiOperation(value = "根据积分类型，扣除积分相关内容")
    @Log(title = "根据积分类型，扣除积分相关内容", businessType = BusinessType.UPDATE)
    @RequestMapping("/deductionPoints")
    public AjaxResult deductionPoints(@RequestBody Map<String,Object> deductionMap)
    {
        return toAjax(pointsRecordService.deductionPoints(deductionMap));
    }

    /**
     * 获取奖励积分
     */
    @ApiOperation(value = "获取奖励积分")
    @Log(title = "获取奖励积分", businessType = BusinessType.UPDATE)
    @RequestMapping("/obtainBonusPoints")
    public AjaxResult obtainBonusPoints(@RequestBody Map<String,Object> obtainBonusPoints)
    {
        return toAjax(pointsRecordService.obtainBonusPoints(obtainBonusPoints));
    }

    /**
     * 获取上月未违规记录列表
     */
    @ApiOperation(value = "获取上月未违规记录列表")
    @RequestMapping("/getLastMonthViolation")
    public AjaxResult getLastMonthViolation(@RequestBody Map<String,Object> obtainBonusPoints)
    {
        return AjaxResult.success(pointsRecordService.getLastMonthViolation(obtainBonusPoints));
    }


    /**
     * 查询积分记录日志详情列表(信用积分)
     */
    @GetMapping("/getPointsLoglist")
    @PreAuthorize(hasPermi = PerPrefix.POINTS_RECORD+ PerFun.CREDITSCORE)
    public TableDataInfo getPointsLoglist(ElPointsLoginfoVO elPointsLoginfoVO) {
        startPage();
        List<ElPointsLoginfo> list = pointsRecordService.getPointsLoglist(elPointsLoginfoVO);
        return getDataTable(list);
    }


    /**
     * 查询积分记录日志详情列表(奖励积分)
     */
    @GetMapping("/getPointsLoglistByReward")
    @PreAuthorize(hasPermi = PerPrefix.POINTS_RECORD+ PerFun.BONUSPOINTS)
    public TableDataInfo getPointsLoglistByReward(ElPointsLoginfoVO elPointsLoginfoVO) {
        startPage();
        List<ElPointsLoginfo> list = pointsRecordService.getPointsLoglist(elPointsLoginfoVO);
        return getDataTable(list);
    }

    /**
     * 查询个人积分记录日志详情列表
     */
    @GetMapping("/getMyPointsLoglist")
    public TableDataInfo getMyPointsLoglist(ElPointsLoginfoVO elPointsLoginfoVO) {
        ElPointsRecord pointsRecord = new ElPointsRecord();
        pointsRecord.setJoinUserId(SecurityUtils.getUserId());
        List<ElPointsRecord> elPointsRecordList = pointsRecordService.selectElPointsRecordList(pointsRecord);
        startPage();
        if(elPointsRecordList.size()>0){
            ElPointsRecord elPointsRecordNew = elPointsRecordList.get(0);
            elPointsLoginfoVO.setPointsId(elPointsRecordNew.getId());
            List<ElPointsLoginfo> list = pointsRecordService.getMyPointsLoglist(elPointsLoginfoVO);
            return getDataTable(list);
        }
       return getDataTable(Collections.emptyList());
    }

    /**
     * 查询个人信息积分情况
     */
    @GetMapping("/getMyPointsLogInfo")
    public AjaxResult getMyPointsLogInfo(ElPointsLoginfoVO elPointsLoginfoVO) {
        ElUserPointsLoginfoVO elUserPointsLoginfoVO = pointsRecordService.getMyPointsLogInfo(elPointsLoginfoVO);
        return AjaxResult.success(elUserPointsLoginfoVO);
    }

    /**
     * 统计所有月份的积分
     */
    @GetMapping("/monthAllCount")
    public AjaxResult monthAllCount(ElPointsLoginfoVO elPointsLoginfoVO) {
        ElPointsCountVO elPointsCountVO = pointsRecordService.monthAllCount(elPointsLoginfoVO);
        return AjaxResult.success(elPointsCountVO);
    }

    /**
     * 个人根据年月统计积分
     */
    @GetMapping("/monthCountPoints")
    public TableDataInfo monthCountPoints(ElPointsLoginfoVO elPointsLoginfoVO) {
        ElPointsRecord pointsRecord = new ElPointsRecord();
        pointsRecord.setJoinUserId(SecurityUtils.getUserId());
        List<ElPointsRecord> elPointsRecordList = pointsRecordService.selectElPointsRecordList(pointsRecord);
        startPage();
        if(elPointsRecordList.size()>0){
            ElPointsRecord elPointsRecordNew = elPointsRecordList.get(0);
            elPointsLoginfoVO.setPointsId(elPointsRecordNew.getId());
            List <ElPointsCountVO> list = pointsRecordService.monthCountPoints(elPointsLoginfoVO);
            return getDataTable(list);
        }
        return getDataTable(Collections.emptyList());
    }

    /**
     * 根据用户id及积分类型查询奖励分记录,并跳转到奖励分页面
     * @param pointsLoginfo
     * @return
     */
    @GetMapping("/myRewardPoint")
    public TableDataInfo myRewardPointList(ElPointsLoginfo pointsLoginfo) {
        startPage();
        pointsLoginfo.setUserId(SecurityUtils.getUserId());
        pointsLoginfo.setPointsType(2L);
        List<ElPointsLoginfo> myRewardPointList = pointsRecordService.myRewardPointList(pointsLoginfo);
        return getDataTable(myRewardPointList);
    }

    @PostMapping("/exits")
    R<Object> findByUserId(@RequestBody Map<String, Object> map){

        List<ElPointsRecordListVO> list = pointsRecordService.findByUserId(Long.valueOf(map.get("userId").toString()));
        return R.ok(list);
    }

    /**
     * 兑换机服务接口
     */
    @PostMapping("/exchangeMachineService")
    public Map<String,String> exchangeMachineService(@RequestParam Map<String,String> machineMap) {
        return pointsRecordService.exchangeMachineService(machineMap);
    }

    /**
     * 兑换机消费奖励分兑换接口
     */
    @PostMapping("/exchangePoints")
    public AjaxResult exchangePoints(@RequestBody String objJson) {
        return AjaxResult.success(pointsRecordService.exchangePoints(objJson));
    }

    /**
     * 兑换机回退商品积分
     */
    @PostMapping("/rollBackPoints")
    public AjaxResult rollBackPoints(@RequestBody String objJson) {
        return AjaxResult.success(pointsRecordService.rollBackPoints(objJson));
    }

}
