package com.goafanti.order.service.impl;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.dao.OrganizationManagementMapper;
import com.goafanti.common.dao.OutsourceOrganizationMapper;
import com.goafanti.common.dao.TOrderLogMapper;
import com.goafanti.common.dao.TOrderNewMapper;
import com.goafanti.common.dao.TOrderOutsourceMapper;
import com.goafanti.common.dao.TOrderTaskMapper;
import com.goafanti.common.enums.NoticeStatus;
import com.goafanti.common.enums.OrderLogProcess;
import com.goafanti.common.model.OutsourceOrganization;
import com.goafanti.common.model.TOrderLog;
import com.goafanti.common.model.TOrderNew;
import com.goafanti.common.model.TOrderTask;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.OrderOutsourceBo;
import com.goafanti.order.bo.OrderOutsourceDtails;
import com.goafanti.order.bo.OutsourceOrganizationBo;
import com.goafanti.order.enums.ProcessStatus;
import com.goafanti.order.service.OrderNewService;
import com.goafanti.order.service.OrderService;
import com.goafanti.order.service.OutsourceOrgService;

@Service
public class OutsourceOrgServiceImpl  extends BaseMybatisDao<TOrderOutsourceMapper> implements OutsourceOrgService {
	@Autowired
	private OutsourceOrganizationMapper  outsourceOrganizationMapper;
	@Autowired
	private OrganizationManagementMapper	organizationManagementMapper;
	@Autowired
	private TOrderOutsourceMapper	tOrderOutsourceMapper;
	@Autowired
	private TOrderNewMapper	tOrderNewMapper;
	@Autowired
	private TOrderTaskMapper	tOrderTaskMapper;
	@Autowired
	private OrderService	orderService;
	@Autowired
	private TOrderLogMapper	tOrderLogMapper;
	@Autowired
	private OrderNewService	orderNewService;
	@Autowired
	private AdminMapper	adminMapper;
	@Override
	public int addOutsourceOrg(OutsourceOrganization o) {
		return outsourceOrganizationMapper.insertSelective(o);
	}
	@Override
	public int updateOutsourceOrg(OutsourceOrganization o) {
		return outsourceOrganizationMapper.updateByPrimaryKeySelective(o);
	}
	@Override
	public int deleteOutsourceOrg(Integer id) {
		return outsourceOrganizationMapper.deleteByPrimaryKey(id);
	}
	@Override
	public List<OutsourceOrganizationBo> selectOutsourceOrg(String orderNo,String tid) {
		
		return outsourceOrganizationMapper.selectOutsourceOrg(orderNo,tid);
	}
	@Override
	public List<OutsourceOrganizationBo> selectOrderOutsourceOrg(String orderNo) {
		
		return outsourceOrganizationMapper.selectOrderOutsourceOrg(orderNo);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public Pagination<OrderOutsourceBo> orderOutsourceList(String name, String contractNo, String starTime,
			String endTime,Integer refundStatus,Integer type, Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		if(pageSize==null||pageSize<0)pageSize=10;
		if(pageNo==null||pageNo<0)pageNo=1;
		if (StringUtils.isNotBlank(name)) params.put("name", name);
		if(refundStatus != null) params.put("refundStatus", refundStatus);
		if(type != null)  params.put("type", type);
		if (StringUtils.isNotBlank(contractNo)) params.put("contractNo", contractNo);
		if (StringUtils.isNotBlank(starTime)) params.put("starTime", starTime);
		if (StringUtils.isNotBlank(endTime)) params.put("endTime", endTime+" 23:59:59");
		Pagination<OrderOutsourceBo> p = (Pagination<OrderOutsourceBo>)findPage("orderOutsourceList", "orderOutsourceCount", params, pageNo, pageSize);
		return p;
	}



	@Override
	public OrderOutsourceDtails orderOutsourceDtails(String orderNo,String tid) {
		return tOrderOutsourceMapper.selectByOrderNo(orderNo,tid);
	}



	@Override
	public int updateAuditOutsource(OrderOutsourceDtails o) {
		o.setAuditTime(new Date());
		List<String> aids = new ArrayList<>();
		Integer type=null;
		TOrderNew t2=tOrderNewMapper.selectByPrimaryKey(o.getOrderNo());
		//外包订单
		if (o.getType()==null||o.getType()==0) {
			o.setType(0);
			TOrderNew t=new TOrderNew();
			t.setOrderNo(o.getOrderNo());
			if (o.getRefundStatus()==1) {
				t.setOrderStatus(2);//订单审核标记通过
				t.setProcessStatus(ProcessStatus.YPCWZY.getCode());
				addOrderLog(o.getOrderNo(),OrderLogProcess.WBTG.getCode(),o.getRemarks());
				aids.add(organizationManagementMapper.selectByPrimaryKey(t2.getOrderDep()).getFinanceId());
				type=NoticeStatus.ORDER_OUTSOURCE_YES.getCode();
			}else {
				t.setOrderStatus(1);
				t.setProcessStatus(ProcessStatus.YPYXGLY.getCode());
				addOrderLog(o.getOrderNo(),OrderLogProcess.WBBH.getCode(),o.getRemarks());
				aids.addAll(adminMapper.listNameByDepAndName(adminMapper.getDeptNameByAid(t2.getSalesmanId()).getDepartmentId(), "营销管理员"));
				type=NoticeStatus.ORDER_OUTSOURCE_NO.getCode();
			}
			tOrderNewMapper.updateByPrimaryKeySelective(t);
		}else if (o.getType()==1) {//项目外包
			TOrderTask tt=tOrderTaskMapper.selectByPrimaryKey(o.getTid());
			aids.add(tt.getTaskReceiver());
			if (o.getRefundStatus()==1) {
				//把项目更改成外包项目
				TOrderTask tt2=new TOrderTask();
				tt2.setId(o.getTid());
				tt2.setOutsource(1);
				tOrderTaskMapper.updateByPrimaryKeySelective(tt2);
				type=NoticeStatus.PROJECT_OUTSOURCE_YES.getCode();
				addOrderLog(o.getOrderNo(),OrderLogProcess.XMWBTG.getCode(),o.getRemarks());
			}else {
				type=NoticeStatus.PROJECT_OUTSOURCE_NO.getCode();
				addOrderLog(o.getOrderNo(),OrderLogProcess.XMWBBH.getCode(),o.getRemarks());
			}
		}
		orderNewService.pushGeneralSendNoticeAndEmail(aids, type,o.getOrderNo(), TokenManager.getAdminId(),t2.getApproval());
		return tOrderOutsourceMapper.updateByPrimaryKeySelective(o);
	}
	
	public void addOrderLog(String orderNo, Integer code,String remarks) {
		TOrderLog tl=new TOrderLog();
		tl.setOrderNo(orderNo);
		tl.setProcess(code);
		tl.setAid(TokenManager.getAdminId());
		tl.setRemarks(remarks);
		tOrderLogMapper.insertSelective(tl);
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrderOutsourceBo> salesmanOrderOutsourceList(String name, String contractNo, String starTime,
			String endTime, Integer refundStatus, Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		if(pageSize==null||pageSize<0)pageSize=10;
		if(pageNo==null||pageNo<0)pageNo=1;
		if (StringUtils.isNotBlank(name)) params.put("name", name);
		params.put("deps", orderService.selectMyDeps());
		if(refundStatus != null) params.put("refundStatus", refundStatus);
		if (StringUtils.isNotBlank(contractNo)) params.put("contractNo", contractNo);
		if (StringUtils.isNotBlank(starTime)) params.put("starTime", starTime);
		if (StringUtils.isNotBlank(endTime)) params.put("endTime", endTime+" 23:59:59");
		Pagination<OrderOutsourceBo> p = (Pagination<OrderOutsourceBo>)findPage("salesmanOrderOutsourceList", "salesmanOrderOutsourceCount", params, pageNo, pageSize);
		return p;
	}

}
