package com.yunfeiyun.agmp.fms.service.impl;

import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.enums.SysWarnEnum;
import com.yunfeiyun.agmp.common.exception.BizException;
import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.common.utils.ip.AddressUtils;
import com.yunfeiyun.agmp.common.utils.ip.IpUtils;
import com.yunfeiyun.agmp.fms.domain.SpmOrder;
import com.yunfeiyun.agmp.fms.domain.TssScanrcd;
import com.yunfeiyun.agmp.fms.domain.TssTracecodeapply;
import com.yunfeiyun.agmp.fms.domain.TssTracecodetpl;
import com.yunfeiyun.agmp.fms.mapper.SpmOrderMapper;
import com.yunfeiyun.agmp.fms.mapper.TssScanrcdMapper;
import com.yunfeiyun.agmp.fms.mapper.TssTracecodeapplyMapper;
import com.yunfeiyun.agmp.fms.mapper.TssTracecodetplMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.SelectCountOfScreenReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.TssScanrcdReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.TssScanrcdResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.TssScanrcdStatResVo;
import com.yunfeiyun.agmp.fms.service.ISysWarnService;
import com.yunfeiyun.agmp.fms.service.ITssScanrcdService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 溯源码扫码记录Service业务层处理
 *
 * @author 杨晓辉
 * @date 2023-07-04
 */
@Service
public class TssScanrcdServiceImpl implements ITssScanrcdService {
    @Autowired
    private TssScanrcdMapper tssScanrcdMapper;

    @Resource
    private TssTracecodeapplyMapper tssTracecodeapplyMapper;
    @Resource
    private SpmOrderMapper spmOrderMapper;
    @Resource
    private TssTracecodetplMapper tssTracecodetplMapper;
    @Resource
    private ISysWarnService sysWarnService;

    /**
     * 查询溯源码扫码记录
     *
     * @param scanrcdId 溯源码扫码记录主键
     * @return 溯源码扫码记录
     */
    @Override
    public TssScanrcd selectTssScanrcdByScanrcdId(String scanrcdId) {
        return tssScanrcdMapper.selectTssScanrcdByScanrcdId(scanrcdId);
    }

    /**
     * 查询溯源码扫码记录列表
     *
     * @param tssScanrcd 溯源码扫码记录
     * @return 溯源码扫码记录
     */
    @Override
    public List<TssScanrcd> selectTssScanrcdList(TssScanrcd tssScanrcd) {
        return tssScanrcdMapper.selectTssScanrcdList(tssScanrcd);
    }

    @Override
    public List<TssScanrcdResVo> selectTssScanrcdList(TssScanrcdReqVo tssScanrcdReqVo) {
        return tssScanrcdMapper.selectTssScanrcdResVo(tssScanrcdReqVo);
    }

    @Override
    public Integer selectCountOfTssScanrcd(SelectCountOfScreenReqVo reqVo) {
        return tssScanrcdMapper.selectCountOfTssScanrcd(reqVo);
    }

    @Override
    public List<TssScanrcdStatResVo> selectCountByProvince(SelectCountOfScreenReqVo reqVo) {
        return tssScanrcdMapper.selectCountByProvince(reqVo);
    }

    @Override
    public List<TssScanrcdStatResVo> selectCountByCity(SelectCountOfScreenReqVo reqVo) {
        return tssScanrcdMapper.selectCountByCity(reqVo);
    }

    /**
     * 新增溯源码扫码记录
     *
     * @param tssScanrcd 溯源码扫码记录
     * @return 结果
     */
    @Override
    public int insertTssScanrcd(TssScanrcd tssScanrcd) {
        return tssScanrcdMapper.insertTssScanrcd(tssScanrcd);
    }

    /**
     * 修改溯源码扫码记录
     *
     * @param tssScanrcd 溯源码扫码记录
     * @return 结果
     */
    @Override
    public int updateTssScanrcd(TssScanrcd tssScanrcd) {
        return tssScanrcdMapper.updateTssScanrcd(tssScanrcd);
    }

    /**
     * 批量删除溯源码扫码记录
     *
     * @param scanrcdIds 需要删除的溯源码扫码记录主键
     * @return 结果
     */
    @Override
    public int deleteTssScanrcdByScanrcdIds(String[] scanrcdIds) {
        return tssScanrcdMapper.deleteTssScanrcdByScanrcdIds(scanrcdIds);
    }

    /**
     * 删除溯源码扫码记录信息
     *
     * @param scanrcdId 溯源码扫码记录主键
     * @return 结果
     */
    @Override
    public int deleteTssScanrcdByScanrcdId(String scanrcdId) {
        return tssScanrcdMapper.deleteTssScanrcdByScanrcdId(scanrcdId);
    }

    @Override
    public void saveScanrcdInfo(String tarcecodeapplyno, HttpServletRequest httpServletRequest) {
        TssScanrcd tssScanrcd = new TssScanrcd();
        TssTracecodeapply tssTracecodeapplyQuery = new TssTracecodeapply();
        tssTracecodeapplyQuery.setTarcecodeapplyno(tarcecodeapplyno);
        List<TssTracecodeapply> tssTracecodeapplys = tssTracecodeapplyMapper.selectTssTracecodeapplyList(tssTracecodeapplyQuery);
        if (tssTracecodeapplys.isEmpty()) {
            throw new BizException(ErrorCode.TSS_TRACE_APPLY_NOT_EXIST);
        }
        TssTracecodeapply tssTracecodeapply = tssTracecodeapplys.get(0);
        TssTracecodetpl tssTracecodetpl = tssTracecodetplMapper.selectTssTracecodetplByTracecodetplId(tssTracecodeapply.getTracecodetplId());
        tssScanrcd.setTarcecodeapplyId(tssTracecodeapply.getTarcecodeapplyId());
        List<TssScanrcd> scanList = tssScanrcdMapper.selectTssScanrcdList(tssScanrcd);
        if (StringUtils.isNotEmpty(tssTracecodeapply.getOrderId())) {
            SpmOrder spmOrder = spmOrderMapper.selectSpmOrderByOrderId(tssTracecodeapply.getOrderId());
            if (null != spmOrder) {
                if (scanList.size() <= tssTracecodetpl.getTracecodetplMaxscan()) {
                    tssScanrcd.setScanrcdId(tssScanrcd.getId());
                    tssScanrcd.setOrderId(tssTracecodeapply.getOrderId());
                    tssScanrcd.setStockId(tssTracecodeapply.getStockId());
                    String realAddress = AddressUtils.getRealAddressByIP(IpUtils.getIpAddr(httpServletRequest));
                    tssScanrcd.setScanrcdAddress(realAddress);
                    if (!StringUtils.isEmpty(realAddress) && realAddress.contains(" ")) {
                        String[] s = realAddress.split(" ");
                        tssScanrcd.setScanrcdProvince(s[0]);
                        tssScanrcd.setScanrcdCity(s[1]);
                    }
                    tssScanrcd.setScanrcdCreateddate(DateUtils.dateTimeNow());
                    tssScanrcdMapper.insertTssScanrcd(tssScanrcd);
                }
                if ((scanList.size() == tssTracecodetpl.getTracecodetplMaxscan())) {
                    String remark = "销售单号：" + spmOrder.getOrderNo() + " 的编号为" + tssTracecodeapply.getTarcecodeapplyno() + "的溯源码存在扫码次数过多异常";
                    sysWarnService.genWarn("", remark, SysWarnEnum.SysWarnType.OVER_MAX_SCAN_TIMES, SysWarnEnum.SysWarnClass.SCAN_CODE_PROCESS);
                }
                if (isDifferentLocation(spmOrder, tssScanrcd)) {
                    String remark = "销售单号：" + spmOrder.getOrderNo() + " 的编号为" + tssTracecodeapply.getTarcecodeapplyno() + "的溯源码存在异地扫码异常";
                    sysWarnService.genWarn("", remark, SysWarnEnum.SysWarnType.OFFSITE_SCAN, SysWarnEnum.SysWarnClass.SCAN_CODE_PROCESS);
                }
            }
        }
    }
    private boolean isDifferentLocation(SpmOrder spmOrder, TssScanrcd tssScanrcd) {
        if (StringUtils.isBlank(spmOrder.getOrderConsigneeprovince()) || StringUtils.isBlank(spmOrder.getOrderConsigneecity())) {
            return false;
        }
        if (StringUtils.isBlank(tssScanrcd.getScanrcdProvince()) || StringUtils.isBlank(tssScanrcd.getScanrcdCity())) {
            return false;
        }
        if (!spmOrder.getOrderConsigneeprovince().equals(tssScanrcd.getScanrcdProvince())) {
            return true;
        }
        if (!spmOrder.getOrderConsigneecity().equals(tssScanrcd.getScanrcdCity())) {
            return true;
        }
        return false;
    }
}
