package com.fs.app.controller;

import cn.hutool.core.util.ObjectUtil;
import com.fs.app.annotation.Login;

import com.fs.his.param.BillListParam;
import com.fs.app.param.FsStoreOrderExpressParam;
import com.fs.app.param.FsStoreOrderFinishParam;
import com.fs.his.vo.FsStoreOrderBillLogVo;
import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;

import com.fs.common.utils.ParseUtils;
import com.fs.his.domain.*;
import com.fs.his.param.*;
import com.fs.his.service.*;
import com.fs.his.vo.FsStoreOrderItemListUVO;
import com.fs.his.vo.FsStoreOrderListUVO;
import com.fs.ybPay.service.IPayService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;


@Api("商城接口")
@RestController
@RequestMapping(value="/app/storeOrder")
public class StoreOrderController extends  AppBaseController {

    @Autowired
    private IFsExpressService expressService;
    @Autowired
    IPayService payService;
    @Autowired
    private IFsStoreOrderService orderService;

    @Autowired
    private IFsStoreOrderItemService orderItemService;

    @Autowired
    private IFsStorePaymentService storePaymentService;
    @Autowired
    private IFsUserService userService;
    @Autowired
    private IFsUserAddressService userAddressService;
    @Autowired
    private IFsUserCouponService userCouponService;
    @Autowired
    private IFsCouponService couponService;
    @Autowired
    private IFsStoreOrderBillLogService orderBillLogService;
    @Login
    @ApiOperation("获取我的订单列表")
    @GetMapping("/getMyStoreOrderList")
        public R getMyStoreOrderList(FsStoreOrderListUParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsStoreOrderListUVO> list=orderService.selectFsStoreOrderListUVO(param);
        PageInfo<FsStoreOrderListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("获取我的订单详情")
    @GetMapping("/getMyStoreOrderById")
    public R getMyStoreOrderById(@RequestParam("orderId") Long orderId, HttpServletRequest request){
        FsStoreOrder order=orderService.selectFsStoreOrderByOrderId(orderId);
//        if(!order.getUserId().equals(Long.parseLong(getUserId()))){
//            return R.error("非法操作");
//        }
        order.setUserPhone(ParseUtils.parsePhone(order.getUserPhone()));
        order.setUserAddress(ParseUtils.parseIdCard(order.getUserAddress()));
        List<FsStoreOrderItemListUVO> list=orderItemService.selectFsStoreOrderItemListUVOByOrderId(orderId);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(order.getCreateTime());
//        String json=configService.selectConfigByKey("his.store");
//        StoreConfig config=JSONUtil.toBean(json,StoreConfig.class);
//        calendar.add(Calendar.MINUTE,config.getUnPayTime());
        SimpleDateFormat format = new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String payLimitTime = format.format(calendar.getTime() );
        //处理是否可以申请售后
        Integer isAfterSales=0;
//        if(order.getStatus().equals(FsStoreOrderStatusEnum.STATUS_4.getValue())) {
//            //已完成订单
//            isAfterSales=1;
//            if (order.getFinishTime() != null) {
////                if (config.getStoreAfterSalesDay() != null && config.getStoreAfterSalesDay() > 0) {
////                    //判断完成时间是否超过指定时间
////                    Calendar calendarAfterSales = new GregorianCalendar();
////                    calendarAfterSales.setTime(order.getFinishTime());
////                    calendarAfterSales.add(calendarAfterSales.DATE, config.getStoreAfterSalesDay()); //把日期往后增加一天,整数  往后推,负数往前移动
////                    if (calendarAfterSales.getTime().getTime() < new Date().getTime()) {
////                        isAfterSales = 0;
////                    }
////                }
//            }
//        }
//        else if(order.getStatus()==2||order.getStatus()==3){
//            isAfterSales=1;
//        }
        return R.ok().put("order",order).put("items",list).put("payLimitTime",payLimitTime);
    }

    @Login
    @ApiOperation("获取订单")
    @GetMapping("/getStoreOrderById")
    public R getStoreOrderById(@RequestParam("orderId") Long orderId, HttpServletRequest request){
        FsStoreOrder order=orderService.selectFsStoreOrderByOrderId(orderId);
//        if(!order.getUserId().equals(Long.parseLong(getUserId()))){
//            return R.error("非法操作");
//        }
        order.setUserPhone(ParseUtils.parsePhone(order.getUserPhone()));
        order.setUserAddress(ParseUtils.parseIdCard(order.getUserAddress()));
        List<FsStoreOrderItemListUVO> list=orderItemService.selectFsStoreOrderItemListUVOByOrderId(orderId);

//        String json=configService.selectConfigByKey("his.store");
//        StoreConfig config=JSONUtil.toBean(json,StoreConfig.class);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(order.getCreateTime());
//        calendar.add(Calendar.MINUTE,config.getUnPayTime());
        SimpleDateFormat format = new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String payLimitTime = format.format(calendar.getTime() );
        return R.ok().put("order",order).put("payLimitTime",payLimitTime).put("items",list);
    }


    @Login
    @ApiOperation("计算订单")
    @PostMapping("/compute")
    public R compute(@Validated @RequestBody FsStoreOrderComputeParam param, HttpServletRequest request){
        return orderService.computeOrder(param);

    }

    @Login
    @ApiOperation("支付")
    @PostMapping("/pay")
    public R pay(HttpServletRequest request, @Validated @RequestBody FsStoreOrderPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return orderService.payOrder(param);
    }




    @Login
    @ApiOperation("支付")
    @PostMapping("/payment")
    public R payment(HttpServletRequest request, @Validated @RequestBody FsStoreOrderDoPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return orderService.payment(param);
    }
    @Login
    @ApiOperation("支付宝支付")
    @PostMapping("/zfbPayment")
    public R zfbPayment(HttpServletRequest request, @Validated @RequestBody FsStoreOrderDoPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return orderService.zfbPayment(param);
    }

    @Login
    @ApiOperation("取消订单")
    @PostMapping("/cancelOrder")
    public R cancelOrder(@Validated @RequestBody FsStoreOrderCancelParam param, HttpServletRequest request){
        FsStoreOrder order=orderService.selectFsStoreOrderByOrderId(param.getOrderId());
        if (ObjectUtil.isNull(order)) {
            throw new CustomException("订单不存在");
        }
        return orderService.cancelOrder(param.getOrderId());

    }
    @Login
    @ApiOperation("快递信息")
    @PostMapping("/getExpress")
    public R getExpress(@Validated @RequestBody FsStoreOrderExpressParam param, HttpServletRequest request){
        FsStoreOrder order=orderService.selectFsStoreOrderByOrderId(param.getOrderId());
        if (ObjectUtil.isNull(order)) {
            throw new CustomException("订单不存在");
        }
        if (order.getStatus() <3) {
            throw new CustomException("未发货订单不能查询");
        }
        return orderService.getExpress(order);
    }

    @Login
    @ApiOperation("完成订单")
    @PostMapping("/finishOrder")
    public R finishOrder(@Validated @RequestBody FsStoreOrderFinishParam param, HttpServletRequest request){
        return orderService.finishOrder(param.getOrderId());
    }


    @Login
    @ApiOperation("获取销售订单列表")
    @GetMapping("/getCompanyUserStoreOrderList")
    public R getCompanyUserStoreOrderList(FsStoreOrderListUParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setCompanyUserId(getCompanyUserId());
        List<FsStoreOrderListUVO> list=orderService.selectFsStoreOrderListUVO(param);
        PageInfo<FsStoreOrderListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }


    @Login
    @ApiOperation("获取订单")
    @GetMapping("/getCompanyUserStoreOrderById")
    public R getCompanyUserStoreOrderById(@RequestParam("orderId") Long orderId, HttpServletRequest request){
        FsStoreOrder order=orderService.selectFsStoreOrderByOrderId(orderId);
//        if(!order.getCompanyUserId().equals(getCompanyUserId())){
//            return R.error("非法操作");
//        }
        order.setUserPhone(ParseUtils.parsePhone(order.getUserPhone()));
        order.setUserAddress(ParseUtils.parseIdCard(order.getUserAddress()));
        List<FsStoreOrderItemListUVO> list=orderItemService.selectFsStoreOrderItemListUVOByOrderId(orderId);

//        String json=configService.selectConfigByKey("his.store");
//        StoreConfig config=JSONUtil.toBean(json,StoreConfig.class);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(order.getCreateTime());
//        calendar.add(Calendar.MINUTE,config.getUnPayTime());
        SimpleDateFormat format = new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String payLimitTime = format.format(calendar.getTime() );
        return R.ok().put("order",order).put("payLimitTime",payLimitTime).put("items",list);
    }




    @Login
    @ApiOperation("开票")
    @PostMapping("/bill")
    public R bill(@RequestBody OrderBillParam param){
        param.setUserId(Long.parseLong(getUserId()));
        return orderBillLogService.bill(param);
    }
    @Login
    @ApiOperation("开票历史")
    @GetMapping("/billList")
    @ApiResponse(code = 200, message = "", responseContainer = "List", response = FsStoreOrderBillLogVo.class)
    public R billList(BillListParam param){
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        PageInfo<FsStoreOrderBillLogVo> listPageInfo= orderBillLogService.billList(param);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @ApiOperation("开票详情")
    @GetMapping("/billInfo")
    @ApiResponse(code = 200, message = "", response = FsStoreOrderBillLog.class)
    public R billList(Long id){
        return R.ok().put("data", orderBillLogService.getById(id));
    }

    @Login
    @ApiOperation("开票冲红")
    @GetMapping("/billBack")
    public R billBack(Long id){
        return orderBillLogService.billBack(id);
    }


}
