package com.org.hktshop.controller.hotking;

import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.hotking.BomOrder;
import com.org.hktshop.entity.user.HktUser;
import com.org.hktshop.service.hotking.BomOrderService;
import com.org.hktshop.utils.DateUtil;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

import java.util.Map;

/**
 * @author owen
 */
@RequestMapping("/")
@RestController
public class BomProductController extends BaseController {

    @Autowired
    private BomOrderService portalBomService;

    /**
     * bom单列表
     */
    @ApiOperation(value = "bom单列表", notes = "bom单列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomOrder", value = "bom單", required = true)
    })
    @RequestMapping(value = "/shop/bom/queryPage")
    public Object queryBomOrderPage(@RequestBody BomOrder bomOrder, HttpServletRequest request) {
        HktUser hktUser = (HktUser) getSessionUserId(request);
        if(hktUser == null){
            return BaseResult.fail(425,"请先登录");
        }
        try {
            bomOrder.setUserId(hktUser.getId());
            bomOrder.setStatus(0);
            return BaseResult.successWithData(portalBomService.queryBomOrderPage(bomOrder));
        } catch (Exception e) {
            logger.error("bom单列表：error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), "bom单列表查询失败");
        }
    }

    /**
     * 删除bom单
     */
    @ApiOperation(value = "删除bom单", notes = "删除bom单", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true)
    })
    @RequestMapping(value = "/shop/bom/delBomOrder")
    public Object delBomOrder(@RequestBody BomOrder bomOrder, HttpServletRequest request) {
        HktUser hktUser = (HktUser) getSessionUserId(request);
        if(hktUser == null){
            return BaseResult.fail(425,"请先登录");
        }
        try {
            bomOrder.setUserId(hktUser.getId());
            portalBomService.delBomOrder(bomOrder);
            return BaseResult.success();
        } catch (Exception e) {
            logger.error("删除bom单：error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), "删除bom单失败");
        }
    }

    /**
     * 修改bom单
     */
    @ApiOperation(value = "修改bom单", notes = "修改bom单", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true)
    })
    @RequestMapping(value = "/shop/bom/updateBomOrder")
    public Object updateBomOrder(@RequestBody BomOrder bomOrder, HttpServletRequest request) {
        HktUser hktUser = (HktUser) getSessionUserId(request);
        if(hktUser == null){
            return BaseResult.fail(425,"请先登录");
        }
        try {
            portalBomService.updateBomOrder(bomOrder);
            return BaseResult.success();
        } catch (Exception e) {
            logger.error("修改bom单：error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), "修改bom单失败");
        }
    }

    /**
     * bom单详情
     */
    @ApiOperation(value = "bom单详情", notes = "bom单详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true)
    })
    @RequestMapping(value = "/shop/bom/findBomOrder")
    public Object findBomOrder(@RequestBody BomOrder bomOrder, HttpServletRequest request) {
        HktUser hktUser = (HktUser) getSessionUserId(request);
        if(hktUser == null){
            return BaseResult.fail(425,"请先登录");
        }
        try {
            bomOrder.setUserId(hktUser.getId());
            return BaseResult.successWithData(portalBomService.findBomOrder(bomOrder));
        } catch (Exception e) {
            logger.error("bom单详情：error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), "bom单详情查询失败");
        }
    }

    /**
     * 根据数量获取实时价格
     */
    @ApiOperation(value = "根据数量获取实时价格", notes = "根据数量获取实时价格", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true)
    })
    @RequestMapping(value = "/shop/bom/getDealPriceByCount")
    public Object getDealPriceByCount(@RequestBody Map<String,String> map) {
        try {
            return BaseResult.successWithData(portalBomService.getDealPriceByCount(map));
        } catch (Exception e) {
            logger.error("根据数量获取实时价格：error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), "根据数量获取实时价格失败");
        }
    }


    /**
     * bom单列表
     */
    @ApiOperation(value = "bom单列表", notes = "bom单列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomOrder", value = "bom單", required = true)
    })
    @RequestMapping(value = "/async/bom/queryPage")
    public Object asyncQueryBomOrderPage(@RequestBody BomOrder bomOrder) {
        try {
            return BaseResult.successWithData(portalBomService.selectBomOrderPage(bomOrder));
        } catch (Exception e) {
            logger.error("bom单列表：error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), "bom单列表查询失败");
        }
    }

    /**
     * bom单详情
     */
    @ApiOperation(value = "bom单详情", notes = "bom单详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true)
    })
    @RequestMapping(value = "/async/bom/findBomOrder")
    public Object asyncFindBomOrder(@RequestBody BomOrder bomOrder) {
        try {
            return BaseResult.successWithData(portalBomService.findBomOrder(bomOrder));
        } catch (Exception e) {
            logger.error("bom单详情：error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), "bom单详情查询失败");
        }
    }
}
