package com.fs.app.controller;


import cn.hutool.json.JSONUtil;
import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.store.config.StoreConfig;
import com.fs.store.domain.FsStoreOrder;
import com.fs.store.enums.OrderLogEnum;
import com.fs.store.param.FsStoreProductQueryParam;
import com.fs.store.service.IFsStoreOrderService;
import com.fs.store.service.IFsStoreOrderStatusService;
import com.fs.store.service.IFsStoreProductService;
import com.fs.store.vo.FsStoreProductListQueryVO;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;


@Api("销售订单")
@RestController
@RequestMapping(value="/app/companyOrder")
public class CompanyOrderController extends  AppBaseController {
    @Autowired
    private IFsStoreOrderService orderService;
    @Autowired
    private ICompanyUserService companyUserService;
    @Autowired
    private IFsStoreProductService productService;
    @Autowired
    private IFsStoreOrderStatusService orderStatusService;
    @Autowired
    private ISysConfigService configService;

    @ApiOperation("获取商品列表")
    @GetMapping("/getSalesProducts")
    public R getSalesProducts(FsStoreProductQueryParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        param.setIsDisplay(0);
        List<FsStoreProductListQueryVO> productList=productService.selectFsStoreProductListQuery(param);
        PageInfo<FsStoreProductListQueryVO> listPageInfo=new PageInfo<>(productList);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("制单")
    @GetMapping("/createSalesOrder")
    public R createSalesOrder(@RequestParam("token")String token,@RequestParam("cateIds")String cateIds, HttpServletRequest request){
        Long userId=redisCache.getCacheObject("company-user-token:"+token);
        if(userId==null){
            return R.error(403,"用户失效");
        }
        CompanyUser companyUser=companyUserService.selectCompanyUserById(userId);
        if(companyUser==null||companyUser.getDelFlag().equals("1")){
            return R.error("用户不存在");
        }
        if(!companyUser.getStatus().equals("0")){
            return R.error("用户已禁用");
        }
        return orderService.createSalesOrder(companyUser,cateIds);
    }

    @ApiOperation("改价")
    @GetMapping("/updateSalseOrderMoney")
    public R updateSalseOrderMoney(@RequestParam("token")String token,@RequestParam("createOrderKey")String createOrderKey, @RequestParam("money") BigDecimal money, HttpServletRequest request){
        Long userId=redisCache.getCacheObject("company-user-token:"+token);
        if(userId==null){
            return R.error(403,"用户失效");
        }
        return orderService.updateSalseOrderMoney(createOrderKey,money);
    }


    @ApiOperation("获取订单")
    @GetMapping("/getSalesOrder")
    public R getSalesOrder(@RequestParam("createOrderKey")String createOrderKey, HttpServletRequest request){
        return orderService.getSalesOrder(createOrderKey);
    }
    @Login
    @ApiOperation("添加用户购物车")
    @GetMapping("/addUserCart")
    public R addUserCart(@RequestParam("createOrderKey")String createOrderKey, HttpServletRequest request){
        return orderService.addUserCart(Long.parseLong(getUserId()),createOrderKey);
    }


    @ApiOperation("改订单价格")
    @GetMapping("/editOrderMoney")
    @Transactional
    public R editOrderMoney(@RequestParam("token")String token,@RequestParam("orderId")Long orderId, @RequestParam("money") BigDecimal money, HttpServletRequest request){
        Long userId=redisCache.getCacheObject("company-user-token:"+token);
        if(userId==null){
            return R.error(403,"用户失效");
        }
        FsStoreOrder order=orderService.selectFsStoreOrderById(orderId);
        if(order.getStatus().equals(0)){
            if(money.compareTo(order.getPayPrice())>0){
                return R.error("金额不能超过订单金额");
            }
            if(money.compareTo(BigDecimal.ZERO)<0){
                return R.error("金额必须大于等于0");
            }
            FsStoreOrder orderMap=new FsStoreOrder();
            orderMap.setId(orderId);
            orderMap.setPayMoney(money);
            orderMap.setPayDelivery(order.getPayPrice().subtract(money));
            if(orderMap.getPayMoney().compareTo(BigDecimal.ZERO)>0&&orderMap.getPayDelivery().compareTo(BigDecimal.ZERO)>0){
                orderMap.setPayType("2");
            }
            else if(orderMap.getPayMoney().compareTo(BigDecimal.ZERO)>0&&orderMap.getPayDelivery().compareTo(BigDecimal.ZERO)==0){
                orderMap.setPayType("1");
            }
            else if(orderMap.getPayMoney().compareTo(BigDecimal.ZERO)==0&&orderMap.getPayDelivery().compareTo(BigDecimal.ZERO)>0){
                orderMap.setPayType("3");
            }
            orderMap.setIsEditMoney(1);
            orderService.updateFsStoreOrder(orderMap);
            orderStatusService.create(orderId, OrderLogEnum.EDIT_ORDER_PRICE.getValue(),
                    OrderLogEnum.EDIT_ORDER_PRICE.getDesc());
            return R.ok("改价成功");
        }
        else{
            return R.error("非法操作");
        }

    }
}
