package com.org.hktshop.controller.portal.product;

import com.org.hktshop.Interface.portal.PortalBomImageService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.hotking.BomImage;
import com.org.hktshop.entity.hotking.BomOrder;
import com.org.hktshop.entity.hotking.BomOrderDetail;
import com.org.hktshop.entity.user.HktMessage;
import com.org.hktshop.entity.user.HktUser;
import com.org.hktshop.service.hotking.BomOrderService;
import com.org.hktshop.service.user.HktMessageService;
import com.org.hktshop.utils.DateUtil;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author hotking03
 */
@RequestMapping("/")
@RestController
public class PortalBomImageController extends BaseController {

    @Autowired
    private PortalBomImageService portalBomImageService;

    @Autowired
    private BomOrderService bomOrderService;

    @Autowired
    private HktMessageService hktMessageService;


    @RequestMapping("/shop/ocr/addBomImage")
    public Object addBomImage(@RequestBody BomImage bomImage, HttpServletRequest request) {
        try {
            HktUser hktUser = (HktUser) getSessionUserId(request);
            if (hktUser == null) {
                return BaseResult.fail(HttpCode.REQUEST_UNDEFINE.getCode(), "请先登录");
            }
            bomImage.setUserId(hktUser.getId());
            portalBomImageService.addBomImage(bomImage);
            return BaseResult.success();
        } catch (Exception e) {
            logger.info("bom图片上传失败  error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    @RequestMapping("/shop/ocr/delBomImage")
    public Object delBomImage(@RequestBody BomImage bomImage, HttpServletRequest request) {
        try {
            HktUser hktUser = (HktUser) getSessionUserId(request);
            if (hktUser == null) {
                return BaseResult.fail(HttpCode.REQUEST_UNDEFINE.getCode(), "请先登录");
            }
            bomImage.setUserId(hktUser.getId());
            portalBomImageService.delBomImage(bomImage);
            return BaseResult.success();
        } catch (Exception e) {
            logger.info("删除失败  error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @RequestMapping("/shop/ocr/findBomImage")
    public Object findBomImage(@RequestBody BomImage bomImage, HttpServletRequest request) {
        try {
            HktUser hktUser = (HktUser) getSessionUserId(request);
            if (hktUser == null) {
                return BaseResult.fail(HttpCode.REQUEST_UNDEFINE.getCode(), "请先登录");
            }
            bomImage.setUserId(hktUser.getId());
            return BaseResult.successWithData(portalBomImageService.findBomImage(bomImage));
        } catch (Exception e) {
            logger.info("Bom图片查询失败  error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @RequestMapping("/shop/ocr/selectBomImage")
    public Object selectBomImage(@RequestBody BomImage bomImage, HttpServletRequest request) {
        try {
            HktUser hktUser = (HktUser) getSessionUserId(request);
            if (hktUser == null) {
                return BaseResult.fail(HttpCode.REQUEST_UNDEFINE.getCode(), "请先登录");
            }
            bomImage.setUserId(hktUser.getId());
            return BaseResult.successWithData(portalBomImageService.selectBomImage(bomImage));
        } catch (Exception e) {
            logger.info("Bom图片列表查询失败  error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    @RequestMapping("/ocr/findBomImage")
    public Object findBomImageById(@RequestBody BomImage bomImage) {
        try {
            return BaseResult.successWithData(portalBomImageService.findBomImage(bomImage));
        } catch (Exception e) {
            logger.info("Bom图片查询失败  error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @RequestMapping("/ocr/updateBomImage")
    public Object updateBomImage(@RequestBody BomOrder bomOrder) {
        try {
            if(bomOrder.getDistribution() == 2 && StringUtils.isNull(bomOrder.getRemark())){
                return BaseResult.fail("请说明无效配单原因");
            }
            bomOrderService.updateBomOrder(bomOrder);

            if(bomOrder.getDistribution() == 2){
                BomOrder bi = bomOrderService.findBomOrderById(bomOrder.getId());
                bomOrderService.distribution(bi);
                HktMessage hktMessage = new HktMessage();
                hktMessage.setUserId(bi.getUserId());
                hktMessage.setTitle("单号："+bi.getUserId()+"无法识别信息！");

                hktMessage.setMessage("您的订单："+bi.getUuid()+"配单失败，原因："+bi.getRemark()+",可咨询人工客服进行精准配单！");

                hktMessage.setSendTime(DateUtil.getDateTime());
                hktMessageService.saveHktMessage(hktMessage);
            }

            return BaseResult.success();
        } catch (Exception e) {
            logger.info("配单修改失败  error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    @RequestMapping("/ocr/findBomImageByPage")
    public Object findBomImageByPage(@RequestBody BomImage bomImage) {
        try {
            return BaseResult.successWithData(portalBomImageService.selectBomImage(bomImage));
        } catch (Exception e) {
            logger.info("Bom图片列表查询失败  error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @RequestMapping("/ocr/distribution")
    public Object distribution(@RequestBody BomOrder bomOrder) {
        try {
            BomOrder bi = bomOrderService.findBomOrderById(bomOrder.getId());
            List<BomOrderDetail> list = bomOrder.getList();
            for (BomOrderDetail bomOrderDetail : list) {
                if (StringUtils.isNull(bomOrderDetail.getBrandName()) && StringUtils.isNull(bomOrderDetail.getParam()) && StringUtils.isNull(bomOrderDetail.getMaterialSn())) {
                    return BaseResult.fail("配单主要信息不能为空");
                }
                bomOrderDetail.setBomId(bi.getUuid());
            }
            bi.setList(list);
            bi.setDistributionUser(bi.getUserId());
            bi.setUpdateTime(DateUtil.getDateTime());
            bi.setDistribution(1);
            bomOrderService.distribution(bi);
            HktMessage hktMessage = new HktMessage();
            hktMessage.setUserId(bi.getUserId());
            hktMessage.setTitle("单号："+bi.getUserId()+" 最新配单消息！");
            hktMessage.setMessage("您的订单："+bi.getUuid()+"已配单成功，请尽快处理，如有疑问可咨询人工客服！");
            hktMessage.setSendTime(DateUtil.getDateTime());
            hktMessageService.saveHktMessage(hktMessage);
            return BaseResult.success();
        } catch (Exception e) {
            logger.info("Bom图片列表查询失败  error={}", e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }
}
