package com.hai.goods.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.dao.GoodsDetailMapper;
import com.hai.dao.GoodsLogisticsMapper;
import com.hai.entity.*;
import com.hai.goods.service.GoodsDetailService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("goodsDetailService")
public class GoodsDetailServiceImpl implements GoodsDetailService {

    @Resource
    private GoodsDetailMapper goodsDetailMapper;

    @Override
    public void insertGoodsDetail(GoodsDetail goodsDetail) {
        goodsDetailMapper.insert(goodsDetail);
    }

    @Override
    public void updateGoodsDetail(GoodsDetail goodsDetail) {
        goodsDetailMapper.updateByPrimaryKey(goodsDetail);
    }

    @Override
    public List<GoodsDetail> getGoodsDetailList(Map<String, Object> map) {

        GoodsDetailExample example = new GoodsDetailExample();
        GoodsDetailExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getString(map, "title") != null) {
            criteria.andNameLike("%" +  MapUtils.getString(map, "title") + "%");
        }
        if (MapUtils.getString(map, "goodsLabel") != null) {
            criteria.andGoodsLabelLike("%" +  MapUtils.getString(map, "goodsLabel") + "%");
        }
        if (MapUtils.getLong(map, "goodsType") != null) {
            criteria.andGoodsTypeEqualTo(MapUtils.getLong(map, "goodsType") );
        }
        if (MapUtils.getLong(map, "opId") != null) {
            criteria.andOpIdEqualTo(MapUtils.getLong(map, "opId") );
        }
        if (MapUtils.getLong(map, "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(map, "companyId") );
        }
        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status") );
        } else {
            criteria.andStatusNotEqualTo(0);
        }

        example.setOrderByClause("create_time desc");
        return goodsDetailMapper.selectByExample(example);
    }

    @Override
    public GoodsDetail findGoodsDetailById(Long id) {
        return goodsDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void upShelfApproveResult(HighApprove highApprove) {
        GoodsDetail goodsDetail = findGoodsDetailById(highApprove.getObjectId());
        if (goodsDetail == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到相关信息！");
        }

        if (goodsDetail.getStatus() != 3) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "状态错误！");
        }

        // 状态:  1:待审批  2:驳回  3:通过
        if (highApprove.getStatus() == 2) {
            goodsDetail.setStatus(2);
        }
        if (highApprove.getStatus() == 3) {
            goodsDetail.setStatus(1);
        }

        goodsDetail.setUpdateTime(new Date());
        updateGoodsDetail(goodsDetail);
    }
}
