package market.Qms.com.service.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.BaseGradeChemMapper;
import market.Qms.com.service.BaseGradeChemService;
import market.Qms.com.vo.BaseGradeChem;
import market.Qms.common.DataHelper;
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="BaseGradeChemService")
public class BaseGradeChemServiceImpl extends BaseServiceImpl<BaseGradeChem, Object> implements BaseGradeChemService {

	@Override
	public ReturnMsg<List<BaseGradeChem>> GetGradeChem(RequestModel<BaseGradeChem> chem) {
		// TODO Auto-generated method stub
		ReturnMsg<List<BaseGradeChem>> msg=new ReturnMsg<>();
		try{
			BaseGradeChemMapper mapper=this.getMapper(BaseGradeChemMapper.class);
			List<BaseGradeChem> list=mapper.find(chem.getModel(),chem.getSkipResults(), chem.getPageSize());
			int count=mapper.countWithCondition(chem.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<BaseGradeChem> InsertGradeChem(BaseGradeChem chem) {
		// TODO Auto-generated method stub
		ReturnMsg<BaseGradeChem> msg=new ReturnMsg<>();
		if(DataHelper.IsNullOrSpace(chem.getChemCode())||DataHelper.IsNullOrSpace(chem.getGradeCode())){
			msg.setMessage("保存数据失败，原因：化学元素或炼钢牌号为空");
			msg.setSuccess(false);
			return msg;
		}
		try{
			BaseGradeChemMapper mapper=this.getMapper(BaseGradeChemMapper.class);
			BaseGradeChem _chem=new BaseGradeChem();
			_chem.setGradeCode(chem.getGradeCode());
			_chem.setChemCode(chem.getChemCode());
			List<BaseGradeChem> _listGradeChem=mapper.find(_chem,0,0);
			if(_listGradeChem.size()>0){
				msg.setMessage("同牌号/化学元素的同一信息已经存在");
				msg.setSuccess(false);
				return msg;
			}
			chem.setCreateTime(new Date());
			mapper.insert(chem);
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(chem);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@Override
	public ReturnMsg<BaseGradeChem> UpdateGradeChem(BaseGradeChem chem) {
		// TODO Auto-generated method stub
		ReturnMsg<BaseGradeChem> msg=new ReturnMsg<>();
		msg.setResult(chem);
		if(DataHelper.IsNullOrSpace(chem.getChemCode())||DataHelper.IsNullOrSpace(chem.getGradeCode())){
			msg.setMessage("保存数据失败，原因：化学元素或炼钢牌号为空");
			msg.setSuccess(false);
			return msg;
		}
		try{
			chem.setUpdateTime(new Date());
			BaseGradeChemMapper mapper=this.getMapper(BaseGradeChemMapper.class);
			mapper.update(chem);
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(chem);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@Override
	public ReturnMsg DelOrRenew(String deleteName,String gradeCode, String chemCode,Boolean flag) {
		ReturnMsg msg=new ReturnMsg<>();
		try{
			// TODO Auto-generated method stub
			BaseGradeChem chem=new BaseGradeChem();
			chem.setGradeCode(gradeCode);
			chem.setChemCode(chemCode);
			chem.setValidflag(flag?"0":"1");
			if(flag){
				chem.setDeleteTime(new Date());
				chem.setDeleteName(deleteName);
			}else{
				chem.setUpdateTime(new Date());
				chem.setUpdateName(deleteName);
			}
			this.getMapper(BaseGradeChemMapper.class).update(chem);
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}
	
	/*****
	 * 2018年2月2日修改
	 * 炼钢牌号管理——化学成分信息查询
	 * @param cp
	 * 传入参数——钢种代码gradeCode
	 * @return
	 */
	public ResultModel doQueryGradeChem(CommonPage<BaseGradeChem> cp){
		ResultModel rm=new ResultModel();
		BaseGradeChem bgc = cp.getObject();
		try {
			rm.setData(this.getMapper(BaseGradeChemMapper.class).doFind(bgc));
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			e.printStackTrace();
		}
		return rm;
	}
	
	/*****
	 * 炼钢牌号管理——化学成分信息保存
	 * @param cp
	 * 传入参数：炼钢化学成分对象
	 * memo1：钢种代码gradeCode
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveGradeChem(CommonPage<BaseGradeChem> cp){
		ResultModel rm=new ResultModel();
		List<BaseGradeChem> chems = cp.getObjectList();
		String gradeCode = cp.getMemo1();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
//			this.getMapper(BaseGradeChemMapper.class).doDelete(gradeCode);
			if (chems != null) {
				for (BaseGradeChem baseGradeChem : chems) {
					if (baseGradeChem.getCreateTime() != null) {
						baseGradeChem.setUpdateName(userName);
						baseGradeChem.setUpdateTime(new Date());
						this.getMapper(BaseGradeChemMapper.class).update(baseGradeChem);
					}else {
						int num = this.getMapper(BaseGradeChemMapper.class).getCount(baseGradeChem);
						if (num != 0) {
							throw new Exception("数据库中已经存在相同化学元素成分！");
						}
						baseGradeChem.setCreateTime(new Date());
						baseGradeChem.setCreateName(userName);
						baseGradeChem.setValidflag("1");
						this.getMapper(BaseGradeChemMapper.class).insert(baseGradeChem);
					}
				}
				rm.setState("200");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("保存失败！");
			e.printStackTrace();
		}
		return rm;
	}
	
	/*****
	 * 炼钢牌号管理——化学成分信息删除
	 * @param cp
	 * 传入参数：钢种代码gradeCode、化学元素代码chemCode
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeleteGradeChem(CommonPage<BaseGradeChem> cp){
		ResultModel rm=new ResultModel();
		BaseGradeChem bgc = cp.getObject();
		try {
			this.getMapper(BaseGradeChemMapper.class).doDelete(bgc);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("删除失败！");
			e.printStackTrace();
		}
		return rm;
	}
	
}
