package com.bonus.gs.sub.evaluate.outsourceEnterprise.service;

import com.bonus.gs.sub.evaluate.manager.utils.AjaxRes;
import com.bonus.gs.sub.evaluate.manager.utils.RegularCheckUtils;
import com.bonus.gs.sub.evaluate.outsourceEnterprise.beans.ImportUserDataVo;
import com.bonus.gs.sub.evaluate.outsourceEnterprise.beans.ViolationBean;
import com.bonus.gs.sub.evaluate.outsourceEnterprise.dao.ViolationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lenovo
 */
@Service("ViolationService")
public class ViolationServiceImpl implements ViolationService {

	@Autowired
	private ViolationDao dao;


	/**
	 * @param o
	 * @return
	 */
	@Override
	public AjaxRes addOutsourceEnterprise(ViolationBean o) {
		AjaxRes ar = new AjaxRes();
		if(!isValidChineseMobileNumber(o.getPhone())){
			ar.setFailMsg("手机号码格式不正确!");
			return ar;
		}

		ViolationBean selected=new ViolationBean();

		selected.setIdCard(o.getIdCard());
		int  isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("身份证号码已存在!");
			return ar;
		}

		selected=new ViolationBean();
		selected.setEnterpriseName(o.getEnterpriseName());
		  isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("企业名称已存在!");
			return ar;
		}

		selected=new ViolationBean();
		selected.setSocialCreditCode(o.getSocialCreditCode());
		isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("统一社会信用代码已存在!");
			return ar;
		}
		selected=new ViolationBean();
		selected.setPhone(o.getPhone());
		isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("手机号码已存在!");
			return ar;
		}

		int num=dao.addOutsourceEnterprise(o);
		if(num>0){
			ar.setSucceedMsg("违章新增成功!");
		}else {
			ar.setFailMsg("新增失败!");
		}

		return ar;
	}
	public static boolean isValidChineseMobileNumber(String number) {
		String regex = "^1[3-9]\\d{9}$";
		return number.matches(regex);
	}

	/**
	 * @param o
	 * @return
	 */
	@Override
	public ViolationBean toViewOutsourceEnterprise(ViolationBean o) {
		return dao.toViewOutsourceEnterprise(o);
	}

	/**
	 * @param o
	 * @return
	 */
	@Override
	public AjaxRes updateOutsourceEnterprise(ViolationBean o) {
		AjaxRes ar = new AjaxRes();
		if(!isValidChineseMobileNumber(o.getPhone())){
			ar.setFailMsg("手机号码格式不正确!");
			return ar;
		}

		ViolationBean selected=new ViolationBean();
		selected.setId(o.getId());
		selected.setIdCard(o.getIdCard());
		int  isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("身份证号码已存在!");
			return ar;
		}

		selected=new ViolationBean();
		selected.setId(o.getId());
		selected.setEnterpriseName(o.getEnterpriseName());
		isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("企业名称已存在!");
			return ar;
		}

		selected=new ViolationBean();
		selected.setId(o.getId());
		selected.setSocialCreditCode(o.getSocialCreditCode());
		isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("统一社会信用代码已存在!");
			return ar;
		}
		selected=new ViolationBean();
		selected.setId(o.getId());
		selected.setPhone(o.getPhone());
		isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("手机号码已存在!");
			return ar;
		}

		int num=dao.updateOutsourceEnterprise(o);
		if(num>0){
			ar.setSucceedMsg("修改成功!");
		}else {
			ar.setFailMsg("修改失败!");
		}

		ar.setSucceedMsg("外包企业修改成功!");
		return ar;
	}

	/**
	 * @param id
	 * @return
	 */
	@Override
	public int outsourceEnterpriseDelete(String[] id) {
		return dao.outsourceEnterpriseDelete(id);
	}

	/**
	 * @param bean
	 * @return
	 */
	@Override
	public List<ViolationBean> exportOutsourceEnterprise(ViolationBean bean) {
		return dao.exportOutsourceEnterprise(bean);
	}

	@Override
	public List<ViolationBean> getOutsourceEnterpriseByPage(ViolationBean o) {
		return dao.getOutsourceEnterpriseByPage(o);
	}

	/**
	 * 导入数据校验
	 * @param socialCreditCode
	 * @param idCard
	 * @param phone
	 * @return
	 */
	@Override
	public Integer getImportDataCheck(ImportUserDataVo dataVo) {
		return dao.getImportDataCheck(dataVo);
	}

	/**
	 * @param enterpriseName
	 * @return
	 */

	/**
	 * @param bean
	 */
	@Override
	public AjaxRes updateShopBaseInfo(ViolationBean bean,int row) {
		AjaxRes ar = new AjaxRes();
		if(!RegularCheckUtils.verificationPhone(bean.getPhone())){
			ar.setFailMsg("第" + row + "行手机号码格式不正确!");
			return ar;
		}
		if (!RegularCheckUtils.verificationIdCard(bean.getIdCard())) {
			ar.setFailMsg("第" + row + "行身份证号码格式不正确!");
			return ar;
		}
		ViolationBean selected=new ViolationBean();
		selected.setIdCard(bean.getIdCard());
		int  isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("第" + row + "行身份证号码已存在!");
			return ar;
		}

		selected=new ViolationBean();
		selected.setEnterpriseName(bean.getEnterpriseName());
		isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("第" + row + "企业名称已存在!");
			return ar;
		}

		selected=new ViolationBean();
		selected.setSocialCreditCode(bean.getSocialCreditCode());
		isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("第" + row + "统一社会信用代码已存在!");
			return ar;
		}
		selected=new ViolationBean();
		selected.setPhone(bean.getPhone());
		isAc =dao.getOutSourceNumById(selected);
		if(isAc>0){
			ar.setFailMsg("第" + row + "手机号码已存在!");
			return ar;
		}
		int num=dao.addShopBaseInfo(bean);
		if(num>0){
			ar.setSucceedMsg("第" + row + "新增成功!");
		}else {
			ar.setFailMsg("第" + row + "新增失败!,前几行数据新增成功");
		}
		return ar;

	}

	/**
	 * @return
	 */



	/**
	 * @param bean
	 * @return
	 */
	@Override
	public List<ViolationBean> exportSubpageList(ViolationBean bean) {
		return dao.exportSubpageList(bean);
	}
}
