package cn.com.ty.lift.ud.expertPunish.controller;

import cn.com.ty.lift.ud.common.DelReq;
import cn.com.xwy.util.collection.ListUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.common.model.PushMessage;
import cn.com.ty.lift.common.model.PushUserInfo;
import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.expertPunish.controller.query.ExpertsPunishmentQuery;
import cn.com.ty.lift.ud.expertPunish.mapper.entity.ExpertsPunishment;
import cn.com.ty.lift.ud.expertPunish.service.IExpertsPunishmentService;
import cn.com.ty.lift.ud.userAccount.service.IUserAccountService;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * 专家惩罚记录API
 * 
 * @author Xson
 * @date 2020-07-08
 */
@Api(tags = "专家惩罚API")
@RestController
@RequestMapping("/api/expertsPunishments")
public class ExpertsPunishmentController extends BaseController<ExpertsPunishment, ExpertsPunishmentQuery> {

    private final Logger log = LoggerFactory.getLogger(ExpertsPunishmentController.class);

    private @Autowired IExpertsPunishmentService expertsPunishmentService;

    private @Autowired IUserInfoService userInfoService;

    private @Autowired IUserAccountService iUserAccountService;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Override
    protected IService<ExpertsPunishment> getService() {
        return expertsPunishmentService;
    }

    // 新建
    @PostMapping("/insert")
    @ApiOperation(value = "专家惩罚")
    @Override
    public RestResponse<ExpertsPunishment> create(@RequestBody ExpertsPunishment entity) {
        if(null == entity.getChargeId()){
            return RestResponse.fail("专家不能为空");
        }
        if(null == entity.getPunishAmount()){
            return RestResponse.fail("处罚金额不能为空");
        }

        RestResponse updateUserBalance =
            userInfoService.updateUserBalance(entity.getChargeId(), entity.getPunishAmount());
        if (updateUserBalance.getStatusCode().equals("1")) {
            entity.setStatuz(0);
        }
        boolean saveFlag = getService().save(entity);
        if (saveFlag) { 
            // 添加消息
            PushUserInfo pushUserInfo = iUserAccountService.getPushUserInfoByUserId(entity.getChargeId().toString());
            PushMessage pushMessage = PushMessage.punishment(entity.getPunishAmount().toString());
            pushMessage.sendTokenOnPlatform(jmsMessagingTemplate, pushUserInfo);

            return RestResponse.success(null, "处罚成功");
        } else {
            return RestResponse.fail("处罚失败");
        }
    }

    @PostMapping("/cancelPunishment")
    @ApiOperation(value = "撤销专家惩罚")
    public RestResponse cancelPunishment(@RequestBody DelReq req) {
        if(ListUtil.isEmpty(req.getIds())){
            return RestResponse.fail("请选择惩罚记录");
        }
        //修改余额
        RestResponse addUserBalance =
                userInfoService.addUserBalance(req.getIds());
        if (addUserBalance.getStatusCode().equals("1")) {
            return RestResponse.success(null, "撤销成功");
        }else{
            return RestResponse.fail("撤销失败");
        }
    }
}