package nccloud.web.pp.supplierprice.action;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.itf.pp.supplierprice.ISupplierPriceService;
import nc.vo.bd.meta.BatchOperateVO;
import nc.vo.pp.supplierprice.entity.SupplierPriceVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pubapp.AppContext;
import nc.vo.scmpub.util.StringUtil;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.framework.web.ui.pattern.grid.GridOperator;

/**
 * 供应商价目表新增修改
 * 
 * @author ligangt
 * @date 2018-4-17
 * @version v1.0
 */
public class SaveAction implements ICommonAction {

	@Override
	public Object doAction(IRequest request) {
		try {
			// 转换前台json
			GridOperator operator = new GridOperator();
			SupplierPriceVO[] vos = operator.toVos(request);

			// 处理前台传过来的修改字段数组，用于保存时校验
			String updateKeysjson = operator.getOriginGrid().getUserjson();
			// 获取修改时有来源的供应商价目表除失效日期外的字段，理应为0，如果有值说明有错误操作
			int updateKeysNum = StringUtil.isSEmptyOrNull(updateKeysjson) ? 0
					: ((JSONArray) JSONObject.parse(updateKeysjson)).size();

			List<SupplierPriceVO> addList = new ArrayList<SupplierPriceVO>();
			List<SupplierPriceVO> updateList = new ArrayList<SupplierPriceVO>();
			List<SupplierPriceVO> deleteList = new ArrayList<SupplierPriceVO>();
			List<SupplierPriceVO> unchangedList = new ArrayList<SupplierPriceVO>();
			// 区分新增和修改
			//zhangxinah增加制单人制单日期修改人修改日期
			String cuserid = InvocationInfoProxy.getInstance().getUserId();
			List<String> orderKey = new ArrayList<>();
			if (vos != null) {
				for (SupplierPriceVO vo : vos) {
					if (VOStatus.NEW == vo.getStatus()) {
						//制单人
						vo.setAttributeValue("vbdef17", cuserid);
						//制单日期
						vo.setAttributeValue("vbdef18", new UFDateTime());
						addList.add(vo);
					} else if (VOStatus.UPDATED == vo.getStatus()) {
						//修改人
						vo.setAttributeValue("vbdef19", cuserid);
						//修改日期
						vo.setAttributeValue("vbdef20", new UFDateTime());
						orderKey.add(vo.getPk_supplierprice());
						updateList.add(vo);
					} else if (VOStatus.DELETED == vo.getStatus()) {
						deleteList.add(vo);
					} else {
						unchangedList.add(vo);
						orderKey.add(vo.getPk_supplierprice());
					}
				}
			}
			// 调用接口保存
			ISupplierPriceService service = ServiceLocator.find(ISupplierPriceService.class);
			BatchOperateVO batchVO = new BatchOperateVO();
			batchVO.setAddObjs(addList.toArray());
			batchVO.setUpdObjs(updateList.toArray());
			batchVO.setDelObjs(deleteList.toArray());
			BatchOperateVO resvo = service.batchSave(batchVO, updateKeysNum);
			// 转换成前台dto
			List<SupplierPriceVO> voList = new ArrayList<SupplierPriceVO>();

			for (String key : orderKey) {
				if (resvo.getUpdObjs() != null) {
					for (Object obj : resvo.getUpdObjs()) {
						SupplierPriceVO vo = (SupplierPriceVO) obj;
						if (key.equals(vo.getPk_supplierprice())) {
							voList.add(vo);
						}
					}
					for (SupplierPriceVO vo : unchangedList) {
						if (key.equals(vo.getPk_supplierprice())) {
							voList.add(vo);
						}
					}
				}
			}
			if (resvo.getAddObjs() != null) {
				Object[] addObjs = resvo.getAddObjs();
				for (Object obj : addObjs) {
					voList.add((SupplierPriceVO) obj);
				}
				// voList.addAll(Arrays.asList(resvo.getAddObjs()));
			}

			vos = voList.toArray(new SupplierPriceVO[voList.size()]);
			if (vos != null && vos.length > 0) {
				Grid grid = operator.toGrid(vos);
				SupplierPricePrecisionProcessor util = new SupplierPricePrecisionProcessor();
				util.process(grid);
				return grid;
			}

		} catch (BusinessException e) {
			ExceptionUtils.wrapException(e);
		}

		return null;
	}

}
