package market.Qms.com.service.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.BaseAdditionalMapper;
import market.Qms.com.service.BaseAdditionalService;
import market.Qms.com.vo.BaseAdditional;
import market.Qms.common.RequestModel;
import market.Qms.common.ReturnMsg;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="BaseAdditionalService")
public class BaseAdditionalServiceImpl extends BaseServiceImpl<BaseAdditional, Object> implements BaseAdditionalService {

	@Override
	public ReturnMsg<List<BaseAdditional>> GetStdAdditional(RequestModel<BaseAdditional> stdAdd) {
		ReturnMsg<List<BaseAdditional>> msg=new ReturnMsg<>();
		try{
			BaseAdditionalMapper mapper=this.getMapper(BaseAdditionalMapper.class);
			List<BaseAdditional> list=mapper.findWithCondition(stdAdd.getModel(), null, "CREATE_TIME DESC", stdAdd.getSkipResults(), stdAdd.getPageSize());
			int count=mapper.countWithCondition(stdAdd.getModel(),null);
			msg.setTotal(count);
			msg.setMessage("获取数据成功");
			msg.setSuccess(true);
			msg.setResult(list);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@Override
	public ReturnMsg<BaseAdditional> SaveStdAdditional(BaseAdditional stdAdd,Boolean flag) {
		ReturnMsg<BaseAdditional> msg=new ReturnMsg<>();
		msg.setResult(stdAdd);
		try{
			BaseAdditionalMapper mapper=this.getMapper(BaseAdditionalMapper.class);
			if(flag){
				stdAdd.setCreateTime(new Date());
				mapper.insert(stdAdd);
			}else{
				stdAdd.setUpdateTime(new Date());
				mapper.update(stdAdd);
			}
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(stdAdd);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@Override
	public ReturnMsg DelOrRenew(String deleteName,String stdCode, String subItemVal, Boolean flag) {
		// TODO Auto-generated method stub
		ReturnMsg msg=new ReturnMsg<>();
		try{
			// TODO Auto-generated method stub
			BaseAdditional add=new BaseAdditional();
			add.setStdCode(stdCode);
			add.setAskSubItmeVal(subItemVal);
			add.setValidflag(flag?"0":"1");
			if(flag){
				add.setDeleteTime(new Date());
				add.setDeleteName(deleteName);
			}else{
				add.setUpdateTime(new Date());
				add.setUpdateName(deleteName);
			}
			this.getMapper(BaseAdditionalMapper.class).update(add);
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}
	
	/**
	 * 批量添加附加要求
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel addStdAdditional(CommonPage<BaseAdditional> oci) throws Exception {
		ResultModel rm = new ResultModel();
		try {
			BaseAdditionalMapper mapper = this.getMapper(BaseAdditionalMapper.class);

			String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
			if (oci != null) {
				
				if (oci.getMemo1() == null || "".equals(oci.getMemo1())) {
					rm.setState("500");
					rm.setMsgInfo("请传入标准号！");
					return rm;
				}

				List<BaseAdditional> additon = oci.getObjectList();
				if (additon == null || additon.size() < 1) {
					this.getMapper(BaseAdditionalMapper.class).deleteByStdCode(oci.getMemo1());
					
				}
				

				// 查询附加要求表，判断该标准下是否存在附加要求
				List<BaseAdditional>  stand =new ArrayList<>();
				stand = this.getMapper(BaseAdditionalMapper.class).findWithCondition(null,
						"STD_CODE='" + oci.getMemo1() + "'", null, 0, 0);

				// 添加所有的附加要求
				if ((stand == null || stand.size() < 1) && (additon != null && additon.size() > 0)) {

					for (BaseAdditional bl : additon) {
						bl.setCreateTime(new Date());
						bl.setCreateName(userName);
						mapper.insert(bl);

					}
					rm.setState("200");
					rm.setMsgInfo("添加成功");
					return rm;
				}

				if ((stand != null && stand.size() > 0) && (additon != null && additon.size() > 0)) {
					for (BaseAdditional bl : additon) {

						if (stand != null && stand.size() > 0) {

							for (int i = 0; i < stand.size(); i++) {

								if ((bl.getStdCode() + bl.getAskSubItmeVal())
										.equals((stand.get(i).getStdCode() + stand.get(i).getAskSubItmeVal()))) {
									mapper.update(bl);
									break;
								}

								if (i == stand.size() - 1) {
									bl.setCreateTime(new Date());
									bl.setCreateName(userName);
									mapper.insert(bl);
								}

							}

						}

					}
					//反向验证
					
					for (BaseAdditional bl : stand) {

						for (int i = 0; i < additon.size(); i++) {
							if ((bl.getStdCode() + bl.getAskSubItmeVal())
									.equals((additon.get(i).getStdCode() + additon.get(i).getAskSubItmeVal()))) {
								break;
							}

							if (i == stand.size() - 1) {
								//删除
								this.getMapper(BaseAdditionalMapper.class).delete(bl.getStdCode(),bl.getAskSubItmeVal());
							}

						}

					}

					rm.setState("200");
					rm.setMsgInfo("添加成功");
					return rm;
					
					
					
				}

				//如果数据库中存在，传入的数据不存在
				if ((stand != null && stand.size() > 0) && (additon == null || additon.size() < 1)) {

					for (BaseAdditional bl : stand) {
						//删除
						this.getMapper(BaseAdditionalMapper.class).delete(bl.getStdCode(),bl.getAskSubItmeVal());
					}

				}

				rm.setData(additon);
			}

			rm.setState("200");

		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}


	
	
	
}
