package cn.com.ty.lift.ud.userOrders.controller;

import java.math.BigDecimal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.chat.mapper.entity.LiftCaseEntity;
import cn.com.ty.lift.ud.chat.service.ILiftCaseService;
import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.utils.UUIDUtil;
import cn.com.ty.lift.ud.coupon.mapper.entity.Coupon;
import cn.com.ty.lift.ud.coupon.service.ICouponService;
import cn.com.ty.lift.ud.liftCaseAppeal.mapper.entity.LiftCaseAppealEntity;
import cn.com.ty.lift.ud.liftCaseAppeal.service.ILiftCaseAppealService;
import cn.com.ty.lift.ud.userOrders.controller.query.UserOrdersQuery;
import cn.com.ty.lift.ud.userOrders.mapper.entity.UserOrders;
import cn.com.ty.lift.ud.userOrders.service.IUserOrdersService;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 订单 API
 * 
 * @author Xson
 * @date 2020-05-23
 */
@Api(tags = "订单 API wrf")
@RestController
@RequestMapping("/api/userOrderss")
public class UserOrdersController extends BaseController<UserOrders,UserOrdersQuery> {

    private final Logger log = LoggerFactory.getLogger(UserOrdersController.class);
    
    private @Autowired
    IUserOrdersService userOrdersService;
    private @Autowired
    ILiftCaseService liftCaseService;
    private @Autowired
    ICouponService couponService;
    private @Autowired ILiftCaseAppealService liftCaseAppealService;
    

    @Override
    protected IService<UserOrders> getService() {
        return userOrdersService;
    }
    
  //新建
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public RestResponse create(@RequestBody UserOrders entity) {
    	entity.setOrderNo(UUIDUtil.getUuidByTime(3));
    	entity.setOrderStatus(0);
        getService().save(entity);
        return RestResponse.success(null, "Created successfully");
    }
    
  //根据条件查询
    @PostMapping("/query-page")
    @ApiOperation(value = "分页查询")
    public RestResponse getCondition(@RequestBody UserOrdersQuery request) {
        IPage<UserOrders> list = getService().page(request.page(), request.condition());
        for(UserOrders uo : list.getRecords()) {
        	LiftCaseEntity byId = liftCaseService.getOneMoreById(uo.getDataId());
        	if(uo.getRedEnvelopeId() != null) {
        		Coupon byId2 = couponService.getById(uo.getRedEnvelopeId());
        		uo.setRedEnvelopeDeduction(byId2.getPrice());
        	}else {
        		uo.setRedEnvelopeDeduction(new BigDecimal("0.00"));
        	}
        	if(uo.getCouponId() != null) {
        		Coupon byId3 = couponService.getById(uo.getCouponId());
        		uo.setCouponDeduction(byId3.getPrice());
        	}else {
        		uo.setCouponDeduction(new BigDecimal("0.00"));
        	}
        	
        	
        	uo.setLiftCaseEntity(byId);
        }
        return RestResponse.success(list, "Queried successfully");
    }
    
  //根据id查详情
    @GetMapping("/query/{id}")
    @ApiOperation(value = "获取详情")
    public RestResponse get(@PathVariable Long id) {
    	UserOrders entity = getService().getById(id);
    	LiftCaseEntity byId = liftCaseService.getOneMoreById(entity.getDataId());
    	LiftCaseAppealEntity byDataId = liftCaseAppealService.getByDataId(byId.getId());
    	if(entity.getRedEnvelopeId() != null) {
    		Coupon byId2 = couponService.getById(entity.getRedEnvelopeId());
    		entity.setRedEnvelopeDeduction(byId2.getPrice());
    	}else {
    		entity.setRedEnvelopeDeduction(BigDecimal.ZERO);
    	}
    	if(entity.getCouponId() != null) {
    		Coupon byId3 = couponService.getById(entity.getCouponId());
    		entity.setCouponDeduction(byId3.getPrice());
    	}else {
    		entity.setCouponDeduction(BigDecimal.ZERO);
    	}
    	entity.setLiftCaseEntity(byId);
    	entity.setLiftCaseAppealEntity(byDataId);
        return RestResponse.success(entity, "Queried successfully");
    }
    
}