package com.iamberry.wechat.welfare;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.coupon.CouponItemDto;
import com.iamberry.wechat.core.entity.welfare.UserAmountLogs;
import com.iamberry.wechat.core.entity.welfare.UserAtLogs;
import com.iamberry.wechat.face.coupon.CouponItemService;
import com.iamberry.wechat.face.coupon.WelfareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author 献
 */
@Controller
@RequestMapping("/wechat/welfare")
public class WelfareController {

    @Autowired
    private WelfareService welfareService;

    @Autowired
    private CouponItemService couponItemService;

    /**
     * 【会员中心】查询优惠券列表，此接口只查询当前用户可用、未赠送的优惠券
     */
    @ResponseBody
    @RequestMapping("/listCoupon")
    public ResultMsg listCoupon(HttpServletRequest request) {
        CouponItemDto cDto = new CouponItemDto();
        cDto.setUseropenid(WechatUtils.getUserBySession(request).getUserOpenid());
        cDto.setCouponUseStatus(1);
        cDto.setBeginDate(new Date());
        cDto.setCouponIsGive(1);

        return ResultMsg.SUCCESS().setData(couponItemService.getCouponItemDtoList(cDto));
    }

    /**
     * 获取优惠券详情，适用于分享后点击进入领取页面
     */
    @ResponseBody
    @RequestMapping("/getCoupon")
    public ResultMsg getCoupon(@RequestParam("id") String couponId) {
        return ResultMsg.SUCCESS().setData(couponItemService.getCouponItemById(couponId));
    }

    /**
     * 领取分享的优惠券
     */
    @ResponseBody
    @RequestMapping("/drawShareCoupon")
    public ResultMsg drawShareCoupon(HttpServletRequest request, @RequestParam("id") String couponId) {
        return welfareService.drawShareCoupon(couponId, WechatUtils.getUserBySession(request).getUserOpenid()) <= 0 ? ResultMsg.ERROR() : ResultMsg.SUCCESS();
    }

    /**
     * 检测用户是否可领取福利
     */
    @ResponseBody
    @RequestMapping("/checkWelfareLogs")
    public ResultMsg checkWelfareLogs(HttpServletRequest req) {
        return welfareService.get(WechatUtils.getUserBySession(req).getUserOpenid()) == null ? ResultMsg.SUCCESS() : ResultMsg.ERROR();
    }

    /**
     * 领取福利
     */
    @ResponseBody
    @RequestMapping("/getWelfare")
    public ResultMsg getWelfare(HttpServletRequest request) {
        return welfareService.draw(WechatUtils.getUserBySession(request).getUserOpenid()) <= 0 ? ResultMsg.ERROR() : ResultMsg.SUCCESS();
    }

    /**
     * 获取用户的金额信息
     */
    @ResponseBody
    @RequestMapping("/getAmount")
    public ResultMsg getAmount(HttpServletRequest request) {
        return ResultMsg.SUCCESS().setData(welfareService.getAmount(WechatUtils.getUserBySession(request).getUserOpenid()));
    }

    /**
     * 查询用户的返利记录
     */
    @ResponseBody
    @RequestMapping("/listAmountLogs")
    public ResultMsg listAmountLogs(UserAmountLogs logs, HttpServletRequest request) {
        logs.setUserOpenid(WechatUtils.getUserBySession(request).getUserOpenid());
        logs.setLogsState(1);
        return ResultMsg.SUCCESS().setData(welfareService.listAmount(logs));
    }

    /**
     * 查询用户的提现记录
     */
    @ResponseBody
    @RequestMapping("/listAtlogs")
    public ResultMsg listAtlogs(HttpServletRequest request) {
        UserAtLogs logs = new UserAtLogs();
        logs.setUserOpenid(WechatUtils.getUserBySession(request).getUserOpenid());
        return ResultMsg.SUCCESS().setData(welfareService.listAt(logs));
    }

    /**
     * 申请提现
     */
    @ResponseBody
    @RequestMapping("/addAtLogs")
    public ResultMsg addAtLogs(UserAtLogs data, HttpServletRequest request) {
        data.setUserOpenid(WechatUtils.getUserBySession(request).getUserOpenid());
        data.setAtState(1);
        return welfareService.addAt(data) <= 0 ? ResultMsg.ERROR() : ResultMsg.SUCCESS();
    }
}
