package com.izouma.awesomeadmin.service.impl;

import java.util.*;

import com.izouma.awesomeadmin.dao.OrderRepairDetailMapper;
import com.izouma.awesomeadmin.dao.OrderRepairMapper;
import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.OrderRepair;
import com.izouma.awesomeadmin.model.OrderRepairDetail;
import com.izouma.awesomeadmin.service.OSSFileService;
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.OrderImage;
import com.izouma.awesomeadmin.service.OrderImageService;
import com.izouma.awesomeadmin.dao.OrderImageMapper;

/**
 * service接口实现类
 */
@Service
public class OrderImageServiceImpl implements OrderImageService {

    private static Logger logger = Logger.getLogger(OrderImageServiceImpl.class);

    @Autowired
    private OrderImageMapper orderImageMapper;

    @Autowired
    private OrderRepairMapper orderRepairMapper;

    @Autowired
    private OrderRepairDetailMapper orderRepairDetailMapper;
    @Autowired
    private OSSFileService OSSFileService;


    @Override
    public List<OrderImage> getOrderImageList(OrderImage record) {

        logger.info("getOrderImageList");
        try {

            return orderImageMapper.queryAllOrderImage(record);
        } catch (Exception e) {
            logger.error("getOrderImageList", e);
        }

        return null;
    }

    @Override
    public List<OrderImage> getOrderImageByPage(Page page, OrderImage record) {

        logger.info("getOrderImageByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            return orderImageMapper.queryOrderImageByPage(parameter);
        } catch (Exception e) {
            logger.error("getOrderImageByPage", e);
        }

        return null;
    }

    @Override
    public OrderImage getOrderImageById(String id) {

        logger.info("getOrderImageyId");
        try {

            return orderImageMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getOrderImageById", e);
        }

        return null;
    }

    @Override
    public OrderImage getOrderImage(OrderImage record) {

        logger.info("getOrderImage");
        try {

            return orderImageMapper.queryOrderImage(record);
        } catch (Exception e) {
            logger.error("getOrderImage", e);
        }

        return null;
    }

    @Override
    public boolean createOrderImage(OrderImage record) {

        logger.info("createOrderImage");
        try {

            int updates = orderImageMapper.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createOrderImage", e);
        }

        return false;
    }

    @Override
    public boolean deleteOrderImage(String id) {

        logger.info("deleteOrderImage");
        try {

            int updates = orderImageMapper.delete(id);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deleteOrderImage", e);
        }

        return false;
    }

    @Override
    public boolean updateOrderImage(OrderImage record) {

        logger.info("updateOrderImage");
        try {

            int updates = orderImageMapper.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }


        } catch (Exception e) {
            logger.error("updateOrderImage", e);
        }

        return false;
    }


    /**
     * 返修
     *
     * @param record
     * @return
     */
    @Override
    public boolean repair(OrderImage record) {

        logger.info("repair");
        try {
            int updates = orderImageMapper.updateByPrimaryKeySelective(record);

            OrderRepair orderRepair = new OrderRepair();
            orderRepair.setOrderId(record.getOrderId());
            List<OrderRepair> orderRepairList = orderRepairMapper.queryAllOrderRepair(orderRepair);

            //当前返修订单。
            OrderRepair nowOrderRepair = new OrderRepair();

            if (orderRepairList.size() > 0) {
                OrderRepair lastRepair = orderRepairList.get(orderRepairList.size() - 1);

                //如果状态为已结束
                if (lastRepair.getStatusFlag() >= 1
                        && lastRepair.getTypeFlag() < 2
                        && !StringUtils.isNotEmpty(record.getDetailRemarkFlag())) {
                    nowOrderRepair.setOrderId(record.getOrderId());
                    nowOrderRepair.setUserId(record.getUserId());
                    nowOrderRepair.setStoreId(record.getStoreId());
                    nowOrderRepair.setTypeFlag(lastRepair.getTypeFlag() + 1);

                    orderRepairMapper.insertSelective(nowOrderRepair);

                } else if (lastRepair.getStatusFlag() == 0) {
                    nowOrderRepair = lastRepair;
                }


            } else {
                nowOrderRepair.setOrderId(record.getOrderId());
                nowOrderRepair.setUserId(record.getUserId());
                nowOrderRepair.setStoreId(record.getStoreId());

                orderRepairMapper.insertSelective(nowOrderRepair);
            }


            OrderRepairDetail orderRepairDetail = new OrderRepairDetail();
            orderRepairDetail.setRepairId(nowOrderRepair.getId() + "");
            orderRepairDetail.setImageId(record.getId() + "");

            orderRepairDetail = orderRepairDetailMapper.queryOrderRepairDetail(orderRepairDetail);
            if (orderRepairDetail != null) {
                orderRepairDetail.setRemark(record.getDetailRemark());
                orderRepairDetailMapper.updateByPrimaryKeySelective(orderRepairDetail);
            } else {

                orderRepairDetail = new OrderRepairDetail();
                orderRepairDetail.setRepairId(nowOrderRepair.getId() + "");
                orderRepairDetail.setImageId(record.getId() + "");

                orderRepairDetail.setRemark(record.getDetailRemark());

                orderRepairDetailMapper.insertSelective(orderRepairDetail);
            }


            if (updates >= 0) {
                return true;
            }

        } catch (Exception e) {
            logger.error("repair", e);
        }

        return false;
    }

    @Override
    public OrderImage getMemorySize(OrderImage record) {

        logger.info("getMemorySize");
        try {

            return orderImageMapper.getMemorySize(record);
        } catch (Exception e) {
            logger.error("getMemorySize", e);
        }

        return null;
    }

    @Override
    public OrderImage getOrderImageTotalNum(OrderImage record) {

        logger.info("getOrderImageTotalNum");
        try {

            return orderImageMapper.getOrderImageTotalNum(record);
        } catch (Exception e) {
            logger.error("getOrderImageTotalNum", e);
        }

        return null;
    }

    @Override
    public Result autoCleanOrderImage(OrderImage record) {

        logger.info("autoCleanOrderImage");
        try {

            List<OrderImage> orderImageList = orderImageMapper.queryAutoCleanOrderImage(record);

            for (OrderImage orderImage : orderImageList) {
                orderImage.setCleanFlag("Y");

                if (StringUtils.isNotEmpty(orderImage.getOriginalImage())) {
                    String artwork = orderImage.getOriginalImage().replace(PropertiesFileLoader.getProperties("aliImageSever") + "/", "");
                    OSSFileService.deleteObject(artwork);
                }
                if (StringUtils.isNotEmpty(orderImage.getOriginalArtwork())) {
                    String artwork = orderImage.getOriginalArtwork().replace(PropertiesFileLoader.getProperties("aliImageSever") + "/", "");
                    OSSFileService.deleteObject(artwork);
                }
                if (StringUtils.isNotEmpty(orderImage.getFinishedImage())) {
                    String artwork = orderImage.getFinishedImage().replace(PropertiesFileLoader.getProperties("aliImageSever") + "/", "");
                    OSSFileService.deleteObject(artwork);
                }
                if (StringUtils.isNotEmpty(orderImage.getFinishedArtwork())) {
                    String artwork = orderImage.getFinishedArtwork().replace(PropertiesFileLoader.getProperties("aliImageSever") + "/", "");
                    OSSFileService.deleteObject(artwork);
                }

                orderImageMapper.updateByPrimaryKeySelective(orderImage);
            }

            return new Result(true, "成功");
        } catch (Exception e) {
            logger.error("autoCleanOrderImage", e);
        }

        return null;
    }
}

