package com.ssj.service.sys.fx.service.impl;

import com.ssj.bean.sys.fx.domain.Device;
import com.ssj.bean.sys.fx.domain.DeviceParts;
import com.ssj.bean.sys.fx.domain.DevicePartsAct;
import com.ssj.bean.sys.fx.domain.Merchant;
import com.ssj.bean.sys.role.domain.Admin;
import com.ssj.dao.sys.fx.dao.DevicePartsDao;
import com.ssj.dao.sys.fx.dao.FxQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.service.sys.fx.service.DevicePartsActService;
import com.ssj.service.sys.fx.service.DevicePartsService;
import com.ssj.service.sys.fx.service.MerchantService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;


@Service
public class DevicePartsServiceImpl extends BaseServiceImpl<DeviceParts, String> implements DevicePartsService {

	@Autowired
	private DevicePartsDao dao;

	@Autowired
	private FxQueryDao fxQueryDao;
	
	@Autowired
	private DevicePartsActService devicePartsActService;
	
	@Autowired
	private MerchantService merchantService;
	
    @Override
    public PagingAndSortingRepository<DeviceParts, String> getDao() {
        return dao;
    }

    @Override
    public Page<Map<String, Object>> findSaleCountList(Map<String, Object> params, Pageable initPage) {
        return fxQueryDao.findSaleCountList(params,initPage);
    }

    @Override
    public Page<Map<String, Object>> salseMouthtList(Map<String, Object> params, Pageable initPage) {

        return fxQueryDao.salseMouthtList(params,initPage);
    }

    @Override
    public Page<Map<String, Object>> salseMouthMoneytList(Map<String, Object> params, Pageable initPage) {
        return fxQueryDao.salseMouthMoneytList(params,initPage);
    }

    @Override
    public Page<Map<String, Object>> salsetList(Map<String, Object> params, Pageable initPage) {
        return fxQueryDao.salsetList(params,initPage);
    }

	@Override
	public Page<DeviceParts> findDevicePartsByPage(Map<String, Object> params,Pageable sp) {
		Map<String, SearchFilter> filters = SearchFilter.parse(params);
		Specification<Device> specification = DynamicSpecifications.bySearchFilter(filters.values(), Device.class);
		return dao.findAll(specification, sp);
	}

	@Override
	public DeviceParts getDevicePartsByPartsId(String partsId, String posCode) {
		return dao.getDevicePartsByPartsId(partsId, posCode);
	}

	@Override
	public void matchDevicePartsByMerchant(String id,Integer matchNum,String posCode,
    		String emsNumber,String emsAddress,String emsUsername,String emsTelephone,Admin admin) {
		DeviceParts fromDeviceParts=dao.getOne(id);
		if(fromDeviceParts!=null){
			Merchant merchant=merchantService.getMerchantByPosCode(posCode);
			DeviceParts toDeviceParts=dao.getDevicePartsByPartsId(fromDeviceParts.getPartsId(), posCode);
			//增加数量
			if(toDeviceParts!=null){
				toDeviceParts.setSellWaitNum(toDeviceParts.getSellWaitNum()+matchNum);
			}else{	
				toDeviceParts=new DeviceParts();
				toDeviceParts.setPartsId(fromDeviceParts.getPartsId());
				toDeviceParts.setCreateTime(new Date());
				toDeviceParts.setUpdateTime(new Date());
				toDeviceParts.setMerchantId(merchant.getId());
				toDeviceParts.setName(fromDeviceParts.getName());
				toDeviceParts.setPartsColor(fromDeviceParts.getPartsColor());
				toDeviceParts.setOrgCode(merchant.getOrgCode());
				toDeviceParts.setPosCode(merchant.getPosCode());
				toDeviceParts.setReturnWaitNum(0);
				toDeviceParts.setSellNum(0);
				toDeviceParts.setSellWaitNum(matchNum);
				dao.save(toDeviceParts);
			}
			//减去数量
			fromDeviceParts.setSellNum(fromDeviceParts.getSellNum()-matchNum);
			dao.save(fromDeviceParts);
			
			DevicePartsAct devicePartsAct=new DevicePartsAct();
			devicePartsAct.setEmsAddress(emsAddress);
			devicePartsAct.setEmsNumber(emsNumber);
			devicePartsAct.setEmsTelephone(emsTelephone);
			devicePartsAct.setEmsUsername(emsUsername);
			devicePartsAct.setMatchNum(matchNum);
			devicePartsAct.setFromMerchantId(fromDeviceParts.getMerchantId());
			devicePartsAct.setOperatorAdminId(admin.getId());
			devicePartsAct.setRemarks("下行分配");
			devicePartsAct.setToMerchantId(merchant.getId());
			devicePartsAct.setType(1);
			devicePartsActService.save(devicePartsAct);
		}	
	}

	@Override
	public void updateDevicePartsNum(String id, Integer type) {
		DeviceParts deviceParts=dao.getOne(id);
		if(deviceParts!=null){
			if(type==1){
				deviceParts.setSellNum(deviceParts.getSellNum()+deviceParts.getSellWaitNum());
				deviceParts.setSellWaitNum(0);
			}else{
				deviceParts.setSellNum(deviceParts.getSellNum()+deviceParts.getReturnWaitNum());
				deviceParts.setReturnWaitNum(0);
			}
			dao.save(deviceParts);
		}
	}

	@Override
	public void returnDevicePartsByMerchant(String id, Integer matchNum,
			String emsNumber, String emsAddress, String emsUsername,String emsTelephone, Admin admin) {
		DeviceParts fromDeviceParts=dao.getOne(id);
		if(fromDeviceParts!=null){
			Merchant fromMerchant=merchantService.getById(fromDeviceParts.getMerchantId());
			if(fromMerchant!=null && StringUtils.isNotEmpty(fromMerchant.getParentPosCode())){
				Merchant toMerchant=merchantService.getMerchantByPosCode(fromMerchant.getParentPosCode());
				DeviceParts toDeviceParts=dao.getDevicePartsByPartsId(fromDeviceParts.getPartsId(), toMerchant.getPosCode());
				//增加数量
				if(toDeviceParts!=null){
					toDeviceParts.setReturnWaitNum(toDeviceParts.getReturnWaitNum()+matchNum);
				}else{	
					toDeviceParts=new DeviceParts();
					toDeviceParts.setPartsId(fromDeviceParts.getPartsId());
					toDeviceParts.setCreateTime(new Date());
					toDeviceParts.setUpdateTime(new Date());
					toDeviceParts.setMerchantId(toMerchant.getId());
					toDeviceParts.setName(fromDeviceParts.getName());
					toDeviceParts.setPartsColor(fromDeviceParts.getPartsColor());
					toDeviceParts.setOrgCode(toMerchant.getOrgCode());
					toDeviceParts.setPosCode(toMerchant.getPosCode());
					toDeviceParts.setReturnWaitNum(matchNum);
					toDeviceParts.setSellNum(0);
					toDeviceParts.setSellWaitNum(0);
					dao.save(toDeviceParts);
				}
				//减去数量
				fromDeviceParts.setSellNum(fromDeviceParts.getSellNum()-matchNum);
				dao.save(fromDeviceParts);
				
				DevicePartsAct devicePartsAct=new DevicePartsAct();
				devicePartsAct.setEmsAddress(emsAddress);
				devicePartsAct.setEmsNumber(emsNumber);
				devicePartsAct.setEmsTelephone(emsTelephone);
				devicePartsAct.setEmsUsername(emsUsername);
				devicePartsAct.setMatchNum(matchNum);
				devicePartsAct.setFromMerchantId(fromDeviceParts.getMerchantId());
				devicePartsAct.setOperatorAdminId(admin.getId());
				devicePartsAct.setRemarks("上行退回");
				devicePartsAct.setToMerchantId(toMerchant.getId());
				devicePartsAct.setType(2);
				devicePartsActService.save(devicePartsAct);
			}	
				
				
			}
			
			

			
			

	}
}
