package com.xinelu.applet.controller.patientcenter;

import com.xinelu.applet.dto.nursingstationgoods.GoodsList;
import com.xinelu.applet.dto.nursingstationgoods.GoodsOrderStationDTO;
import com.xinelu.applet.dto.patientcenter.PatientCenterCouponDTO;
import com.xinelu.applet.service.patientcenter.PatientCenterService;
import com.xinelu.applet.vo.coupon.CouponVO;
import com.xinelu.applet.vo.patientcenter.PatientCenterCouponVO;
import com.xinelu.common.annotation.MobileRequestAuthorization;
import com.xinelu.common.annotation.RepeatSubmit;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.manage.dto.subscribemessagerecord.SubscribeMessageRecordDTO;
import com.xinelu.manage.service.patientinfo.IPatientInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 被护理人基本信息Controller
 *
 * @author zhangheng
 * @date 2022-09-02
 */
@RestController
@RequestMapping("/nurseApplet/patientInfo")
public class PatientCenterController extends BaseController {

    @Resource
    private IPatientInfoService patientInfoService;
    @Resource
    private PatientCenterService patientCenterService;

    /**
     * 根据登录id查询个人信息
     *
     * @param id 被护理人id
     * @return 护理人信息
     */
    @GetMapping(value = "/id")
    public AjaxResult getInfo(Long id) {
        if (Objects.isNull(id)) {
            return AjaxResult.error("被护理人编号不能为空!");
        }
        return AjaxResult.success(patientInfoService.selectPatientInfoById(id));
    }

    /**
     * 会员app和微信小程序-签到方法
     *
     * @param patientId     会员用户id
     * @param signInChannel 签到来源，微信小程序、会员APP
     * @return 签到结果
     */
    @MobileRequestAuthorization
    @PostMapping("/signIn")
    public AjaxResult patientSignIn(Long patientId, String signInChannel) {
        if (Objects.isNull(patientId)) {
            return AjaxResult.error("会员信息不能为空！");
        }
        if (StringUtils.isBlank(signInChannel)) {
            return AjaxResult.error("签到方式不能为为空！");
        }
        return patientCenterService.patientSignIn(patientId, signInChannel);
    }

    /**
     * 个人中心优惠券(共用)
     *
     * @param patientCenterCouponVO 用户信息及优惠券状态
     * @return List<CouponVO>
     */
    @MobileRequestAuthorization
    @GetMapping("/selectCoupon")
    public TableDataInfo selectCoupon(@Validated(Insert.class) PatientCenterCouponVO patientCenterCouponVO) {
        startPage();
        List<CouponVO> list = patientCenterService.selectCouponByUseStatus(patientCenterCouponVO);
        return getDataTable(list);
    }

    /**
     * 个人中心积分兑换-查询可以兑换的商品
     *
     * @return List<GoodInfoVO>
     */
    @MobileRequestAuthorization
    @GetMapping("selectExchangeGoods")
    public TableDataInfo selectExchangeGoods() {
        startPage();
        List<GoodsList> list = patientCenterService.selectExchangeGoods();
        return getDataTable(list);
    }

    /**
     * 获取积分返回规则
     *
     * @param patientId 用户id
     * @return com.xinyilu.nurseapplet.domain.vo.patientcenter.PatientSignInfoVO
     **/
    @MobileRequestAuthorization
    @GetMapping("selectPatientSignIn")
    public AjaxResult selectPatientSignIn(Long patientId) {
        if (Objects.isNull(patientId)) {
            return AjaxResult.error("会员信息不能为空！");
        }
        return AjaxResult.success(patientCenterService.selectPatientSignIn(patientId));
    }

    /**
     * 微信小程序-邀请好友接口
     *
     * @param inviteId 邀请人id
     * @return 邀请码图片地址
     */
    @MobileRequestAuthorization
    @PostMapping("/inviteFriends")
    public AjaxResult inviteFriends(Long inviteId) {
        if (Objects.isNull(inviteId)) {
            return AjaxResult.error("邀请人信息不能为空！");
        }
        return patientCenterService.inviteFriends(inviteId);
    }

    /**
     * 新人福利优惠券(共用)
     *
     * @param patientCenterCouponDTO 用户信息及优惠券状态
     * @return List<CouponVO>
     */
    @MobileRequestAuthorization
    @GetMapping("/couponByUseStatus")
    public TableDataInfo couponByUseStatus(PatientCenterCouponDTO patientCenterCouponDTO) {
        return patientCenterService.getCouponByUseStatus(patientCenterCouponDTO);
    }

    /**
     * 新人福利-优惠券领取
     *
     * @param patientId 用户id
     * @param couponId  优惠券id
     * @return AjaxResult
     */
    @MobileRequestAuthorization
    @PostMapping("/insertCouponReceive")
    public AjaxResult insertCouponReceive(Long patientId, Long couponId) {
        if (Objects.isNull(patientId) || Objects.isNull(couponId)) {
            return AjaxResult.error("用户信息或优惠券信息不能为空！");
        }
        return patientCenterService.insertCouponReceive(patientId, couponId);
    }

    /**
     * 手机App和微信小程序积分兑换订单方法
     *
     * @param goodsOrder 商品订单
     * @return 结果
     */
    @MobileRequestAuthorization
    @RepeatSubmit
    @PostMapping("/integralGoodsOrder")
    public AjaxResult integralGoodsOrder(@Validated(Insert.class) @RequestBody GoodsOrderStationDTO goodsOrder, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ServiceException(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        if (Objects.isNull(goodsOrder.getIntegralExchangeSill()) || goodsOrder.getIntegralExchangeSill() <= 0) {
            return AjaxResult.error("当前健康豆数值不足，无法兑换！");
        }
        if (Objects.isNull(goodsOrder.getIntegralExchangeCount()) || goodsOrder.getIntegralExchangeCount() <= 0) {
            return AjaxResult.error("兑换商品数量不正确，无法兑换！");
        }
        return patientCenterService.insertIntegralGoodsOrder(goodsOrder);
    }

    /**
     * 新增订阅微信小程序订阅消息记录，暂时走微信回调事件，未使用
     *
     * @param subscribeMessageRecord 订阅微信小程序订阅消息记录
     * @return com.xinyilu.common.core.domain.AjaxResult
     **/
    @MobileRequestAuthorization
    @PostMapping("/insertSubscribeMessageRecord")
    public AjaxResult insertSubscribeMessageRecord(@RequestBody SubscribeMessageRecordDTO subscribeMessageRecord) {
        if (CollectionUtils.isEmpty(subscribeMessageRecord.getSubscribeMessageRecordList())) {
            return AjaxResult.error("订阅信息不能为空！");
        }
        return patientCenterService.insertSubscribeMessageRecord(subscribeMessageRecord.getSubscribeMessageRecordList());
    }
}
