package com.fs.app.controller;


import com.alibaba.fastjson.JSON;
import com.fs.app.annotation.Login;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.his.domain.*;
import com.fs.his.enums.FsUserIntegralLogTypeEnum;
import com.fs.his.enums.PaymentMethodEnum;
import com.fs.his.param.*;
import com.fs.his.service.*;
import com.fs.his.vo.*;
import com.fs.system.service.ISysConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Slf4j
@Api("积分接口")
@RestController
@RequestMapping(value="/app/integral")
public class IntegralController extends  AppBaseController {

    @Autowired
    private IFsIntegralGoodsService goodsService;
    @Autowired
    private IFsUserIntegralLogsService userIntegralLogsService;
    @Autowired
    private IFsIntegralOrderService integralOrderService;
    @Autowired
    private IFsUserService userService;
    @Autowired
    private IFsUserSignService userSignService;
    @Autowired
    private IFsIntegralCartService cartService;
    @Autowired
    private ISysConfigService configService;
    @ApiOperation("获取积分商品列表")
    @GetMapping("/getIntegralGoodsList")
    @Cacheable(value = "getIntegralGoodsList", key = "#param")
    public R getIntegralGoodsList(FsIntegralGoodsListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsIntegralGoodsListUVO> list=goodsService.selectFsIntegralGoodsListUVO(param);
        PageInfo<FsIntegralGoodsListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @ApiOperation("获取积分商品详情")
    @GetMapping("/getIntegralGoodsById")
    @Cacheable(value = "getIntegralGoodsById", key = "#goodsId")
    public R getIntegralGoodsById(@RequestParam("goodsId")Long goodsId, HttpServletRequest request){
        FsIntegralGoods goods=goodsService.selectFsIntegralGoodsByGoodsId(goodsId);
        return R.ok().put("data",goods);
    }

    @Login
    @ApiOperation("获取积分记录")
    @GetMapping("/getUserIntegralLogsList")
    public R getUserIntegralLogsList(FsUserIntegralLogsListUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserIntegralLogsListUVO> list=userIntegralLogsService.selectFsUserIntegralLogsListUVO(param);
        PageInfo<FsUserIntegralLogsListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("获取积分订单列表")
    @GetMapping("/getIntegralOrderList")
    public R getIntegralOrderList(FsIntegralOrderListUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsIntegralOrderListUVO> list=integralOrderService.selectFsIntegralOrderListUVO(param);
        PageInfo<FsIntegralOrderListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @ApiOperation("获取积分订单详情")
    @GetMapping("/getIntegralOrderById")
    public R getIntegralOrderById(@RequestParam("orderId")Long orderId, HttpServletRequest request){
        FsIntegralOrder order=integralOrderService.selectFsIntegralOrderByOrderId(orderId);
        return R.ok().put("data",order);
    }


    @Login
    @ApiOperation("创建订单")
    @PostMapping("/createOrder")
    @RepeatSubmit
    public R createOrder(@Validated @RequestBody FsIntegralOrderCreateParam param){
        param.setUserId(Long.parseLong(getUserId()));
        return integralOrderService.createOrder(param);
    }

    @Login
    @ApiOperation("取消订单")
    @PostMapping("/cannelOrder")
    @RepeatSubmit
    public R cannelOrder(@Validated @RequestBody FsIntegralOrderDoPayParam param){
        return integralOrderService.cannelOrder(param.getOrderId());
    }

    @Login
    @RepeatSubmit
    @ApiOperation("支付")
    @PostMapping("/payment")
    public R payment(@Validated @RequestBody FsIntegralOrderDoPayParam param) {
        param.setUserId(Long.parseLong(getUserId()));
        return integralOrderService.payment(param, PaymentMethodEnum.MINIAPP_WECHAT);
    }

    @Login
    @RepeatSubmit
    @ApiOperation("支付宝支付")
    @PostMapping("/aliPayment")
    public R aliPayment(@Validated @RequestBody FsIntegralOrderDoPayParam param) {
        param.setUserId(Long.parseLong(getUserId()));
        return integralOrderService.payment(param, PaymentMethodEnum.ALIPAY);
    }

    @Login
    @RepeatSubmit
    @ApiOperation("H5微信支付")
    @PostMapping("/H5WxPayment")
    public R h5WxPayment(@Validated @RequestBody FsIntegralOrderDoPayParam param) {
        param.setUserId(Long.parseLong(getUserId()));
        return integralOrderService.payment(param, PaymentMethodEnum.H5_WECHAT);
    }

    @Login
    @RepeatSubmit
    @ApiOperation("支付宝支付")
    @PostMapping("/H5AliPayment")
    public R h5AliPayment(@Validated @RequestBody FsIntegralOrderDoPayParam param) {
        param.setUserId(Long.parseLong(getUserId()));
        return integralOrderService.payment(param, PaymentMethodEnum.H5_ALIPAY);
    }

    @Login
    @ApiOperation("获取用户签到记录")
    @GetMapping("/getUserSign")
    public R getUserSign(HttpServletRequest request){
        FsUser user=userService.selectFsUserByUserId(Long.parseLong(getUserId()));
        //获取签到配置
        String json=configService.selectConfigByKey("his.sign");
        //判断用户昨天是否签到过
        Long signNum=userSignService.getSign(user);
        Boolean isDaySign=userSignService.isDaySign(user);
        return R.ok().put("isDaySign", isDaySign).put("signNum", signNum).put("integral",user.getIntegral().intValue()).put("sign", json);
    }
    @Login
    @ApiOperation("签到")
    @PostMapping("/sign")
    public R sign(HttpServletRequest request){
        FsUser user=userService.selectFsUserByUserId(Long.parseLong(getUserId()));
        Long integral=userSignService.sign(user);
        return R.ok("签到获得" + integral + "积分");
    }

    @Login
    @ApiOperation("获取积分")
    @PostMapping("/addIntegral")
    public R addIntegral(@RequestBody FsUserAddIntegralParam param){
        param.setUserId(Long.parseLong(getUserId()));
        return userIntegralLogsService.addIntegral(param);
    }

    @Login
    @ApiOperation("获取新人福利完成情况")
    @GetMapping("/getNewcomerBenefits")
    public R getNewcomerBenefits(){
       return userIntegralLogsService.getNewcomerBenefits(Long.parseLong(getUserId()));
    }

    @Login
    @ApiOperation("分享健康知识获取积分")
    @PostMapping("/shareHealth")
    public R shareHealth(@RequestBody FsUserAddIntegralTemplateParam fsUserAddIntegralTemplateParam){
        fsUserAddIntegralTemplateParam.setUserId(Long.parseLong(getUserId()));
        fsUserAddIntegralTemplateParam.setLogType(FsUserIntegralLogTypeEnum.TYPE_3.getValue());
        return userIntegralLogsService.addIntegralTemplate(fsUserAddIntegralTemplateParam);
    }

    @Login
    @ApiOperation("添加或修改购物车")
    @PostMapping("/addOrUpdateCart")
    public R addOrUpdateCart(@Validated @RequestBody FsIntegralCartParam param) {
        log.debug("addOrUpdateCart param: {}", JSON.toJSONString(param));
        cartService.addOrUpdateCart(Long.parseLong(getUserId()), param.getGoodsId(), param.getCartNum(), param.getIsCart());
        return R.ok();
    }

    @Login
    @ApiOperation("删除购物车")
    @PostMapping("/delCart")
    public R delCart(@RequestBody List<Long> ids) {
        log.debug("delCartByIds ids: {}", ids);
        if (Objects.isNull(ids) || ids.isEmpty()) {
            return R.error("ID不能为空");
        }
        cartService.delCartByIds(Long.parseLong(getUserId()), ids);
        return R.ok();
    }

    @Login
    @ApiOperation("获取购物车商品数量")
    @GetMapping("/getCartCount")
    public R getCartCount(@RequestParam(required = false) Long goodsId) {
        log.debug("getCartCount goodsId: {}", goodsId);
        Map<String,Object> params = new HashMap<>();
        params.put("userId", Long.parseLong(getUserId()));
        params.put("goodsId",goodsId);
        return R.ok().put("data", cartService.getCartCountByMap(params));
    }

    @Login
    @ApiOperation("获取购物车列表")
    @GetMapping("/getCarts")
    public R getCarts() {
        return R.ok().put("carts", cartService.getCartsByUserId(Long.parseLong(getUserId())));
    }

    @Login
    @ApiOperation("根据ID集合查询购物车")
    @PostMapping("/getCartByIds")
    public R getCartByIds(@RequestBody List<Long> ids) {
        return R.ok().put("data", cartService.getCartByIds(Long.parseLong(getUserId()), ids));
    }

    @Login
    @ApiOperation("创建购物车订单")
    @PostMapping("/createCartOrder")
    @RepeatSubmit
    public R createCartOrder(@Validated @RequestBody FsIntegralCartOrderCreateParam param){
        param.setUserId(Long.parseLong(getUserId()));
        return integralOrderService.createCartOrder(param);
    }

}
