package com.xinelu.applet.controller.nursingstationgoods;

import com.xinelu.applet.dto.nursingstationgoods.GoodsList;
import com.xinelu.applet.dto.nursingstationgoods.GoodsOrderPatient;
import com.xinelu.applet.dto.nursingstationgoods.GoodsOrderStationDTO;
import com.xinelu.applet.service.nursingstationgoods.INursingStationGoodsService;
import com.xinelu.applet.vo.nursingstationgoods.UpdateDefaultAddressVO;
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.custominterface.Update;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.manage.domain.goodsCategory.GoodsCategory;
import com.xinelu.manage.domain.receiveAddressInfo.ReceiveAddressInfo;
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.math.BigDecimal;
import java.util.List;
import java.util.Objects;


/**
 * 商品信息页面控制器
 *
 * @author ljh
 * @version 1.0
 * Create by 2022/10/17 13:40
 */

@RestController
@RequestMapping("/nurseApplet/nursingStationGoods")
public class NursingStationGoodsController extends BaseController {

    @Resource
    private INursingStationGoodsService nursingStationGoodsService;

    /**
     * 查询商品分类信息列表
     */
    @MobileRequestAuthorization
    @GetMapping("/goodsCategoryList")
    public TableDataInfo goodsCategoryList(GoodsCategory goodsCategory) {
        startPage();
        List<GoodsCategory> goodsCategoryList = nursingStationGoodsService.getGoodsCategoryList(goodsCategory);
        return getDataTable(goodsCategoryList);
    }

    /**
     * 查询商品二级分类信息列表
     */
    @MobileRequestAuthorization
    @GetMapping("/getGoodsCategoryNameList")
    public AjaxResult getGoodsCategoryNameList(Long goodsCategoryId) {
        if (Objects.isNull(goodsCategoryId)) {
            return AjaxResult.error("商品分类信息id不能为空！");
        }
        return nursingStationGoodsService.getGoodsCategoryNameList(goodsCategoryId);
    }

    /**
     * 根据商品分类id查询商品信息
     *
     * @param goodsList 商品信息
     * @return com.xinyilu.common.core.page.TableDataInfo
     **/
    @MobileRequestAuthorization
    @GetMapping("/goodsList")
    public TableDataInfo goodsList(GoodsList goodsList) {
        return nursingStationGoodsService.getGoodsInfoList(goodsList);
    }


    /**
     * 根据商品id查询商品详细信息
     *
     * @param goodsInfoId 商品详细id
     * @param patientId   用户信息id
     * @return com.xinyilu.nurseapplet.domain.nearbynursingstation.NearbyNursingStationVO
     **/
    @MobileRequestAuthorization
    @GetMapping("/goodsDetails")
    public AjaxResult goodsDetails(Long goodsInfoId, Long patientId) {
        if (Objects.isNull(goodsInfoId)) {
            return AjaxResult.error("商品id不能为空！");
        }
        return AjaxResult.success(nursingStationGoodsService.getGoodsDetails(goodsInfoId, patientId));
    }


    /**
     * 手机App和微信小程序商城确认订单方法
     *
     * @param goodsOrder 商品订单
     * @return 结果
     */
    @MobileRequestAuthorization
    @RepeatSubmit
    @PostMapping("/addStationGoodsOrder")
    public AjaxResult addStationGoodsOrder(@Validated(Insert.class) @RequestBody GoodsOrderStationDTO goodsOrder, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ServiceException(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        if (Objects.nonNull(goodsOrder.getTotalPrice()) && goodsOrder.getTotalPrice().compareTo(BigDecimal.ZERO) <= 0) {
            return AjaxResult.error("订单金额不正确，无法下单，请输入正确的订单金额！");
        }
        return nursingStationGoodsService.insertNurseStationGoodsOrder(goodsOrder);
    }


    /**
     * 根据订单编号查询订单信息（废弃，暂时不用）
     *
     * @param orderNo 订单信息编号
     * @return com.xinyilu.nurseapplet.domain.nursingStationGoods.GoodsOrderPatient
     **/
    @GetMapping("/orderPatient")
    public AjaxResult orderPatient(String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return AjaxResult.error("订单编号不能为空！");
        }
        GoodsOrderPatient orderPatient = nursingStationGoodsService.selectGoodsOrderPatient(orderNo);
        return AjaxResult.success(orderPatient);
    }

    /**
     * 根据被护理人id查询基本信息
     *
     * @param patientId 被护理人id
     * @return com.xinyilu.base.domain.patientinfo.PatientInfo
     **/
    @MobileRequestAuthorization
    @GetMapping("/goodPatientInfo")
    public AjaxResult goodPatientInfo(Long patientId) {
        if (Objects.isNull(patientId)) {
            return AjaxResult.error("会员信息不能为空！");
        }
        return nursingStationGoodsService.getReceiveAddressInfoList(patientId);
    }

    /**
     * 获取收货人地址信息详细信息
     */
    @MobileRequestAuthorization
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(nursingStationGoodsService.selectReceiveAddressInfoById(id));
    }


    /**
     * 新增收货人地址信息
     */
    @MobileRequestAuthorization
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody ReceiveAddressInfo receiveAddressInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ServiceException(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return toAjax(nursingStationGoodsService.insertReceiveAddressInfo(receiveAddressInfo));
    }


    /**
     * 修改收货人地址信息
     */
    @MobileRequestAuthorization
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody ReceiveAddressInfo receiveAddressInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ServiceException(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return toAjax(nursingStationGoodsService.updateReceiveAddressInfo(receiveAddressInfo));
    }

    /**
     * 删除收货人地址信息
     */
    @MobileRequestAuthorization
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nursingStationGoodsService.deleteReceiveAddressInfoByIds(ids));
    }


    /**
     * 获取区域详细信息
     */
    @MobileRequestAuthorization
    @GetMapping("/getSubordinateRegions")
    public AjaxResult getSubordinateRegionsFindSuperiorRegions(@RequestParam("areaCode") String areaCode) {
        if (StringUtils.isBlank(areaCode)) {
            return AjaxResult.error("区域编码不能为空！");
        }
        return AjaxResult.success(nursingStationGoodsService.getSubordinateRegionsFindSuperiorRegions(areaCode));
    }

    /**
     * App和微信小程序确认收货
     *
     * @param orderNo 订单编号
     * @return 返回结果
     */
    @MobileRequestAuthorization
    @PostMapping("/confirmReceipt")
    public AjaxResult confirmReceipt(String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return AjaxResult.error("请选择订单信息！");
        }
        return nursingStationGoodsService.confirmReceipt(orderNo);
    }

    /**
     * 微信小程序和会员App设置默认地址接口
     *
     * @param defaultAddressList 默认地址标识集合
     * @return 结果
     */
    @PostMapping("/updateDefaultAddress")
    public AjaxResult updateDefaultAddress(@RequestBody @Validated(Update.class) List<UpdateDefaultAddressVO> defaultAddressList) {
        if (CollectionUtils.isEmpty(defaultAddressList)) {
            return AjaxResult.error("请选择要设置的收货地址信息！");
        }
        return nursingStationGoodsService.updateDefaultAddress(defaultAddressList);
    }
}
