package com.goafanti.order.service.impl;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.*;
import com.goafanti.common.enums.NoticeStatus;
import com.goafanti.common.enums.OrderLogProcess;
import com.goafanti.common.model.*;
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.*;
import com.goafanti.order.enums.ProcessStatus;
import com.goafanti.order.service.OrderNewService;
import com.goafanti.order.service.OrderService;
import com.goafanti.order.service.OutsourceOrgService;
import com.goafanti.organization.bo.ProjectTypePuls;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class OutsourceOrgServiceImpl  extends BaseMybatisDao<TOrderOutsourceMapper> implements OutsourceOrgService {
	@Autowired
	private OutsourceOrganizationMapper  outsourceOrganizationMapper;
	@Autowired
	private DepartmentMapper departmentMapper;
	@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;
	@Autowired
	private OutsourceLogMapper	outsourceLogMapper;
	@Autowired
	private OrderNewService	OrderNewService;
	@Autowired
	private TTaskMidMapper	tTaskMidMapper;
	@Autowired
	private TOrderMidMapper	tOrderMidMapper;








	@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 tid,
			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(type != null)  params.put("tid", tid);
		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.queryById(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.YPCW_JSZJ.getCode());
				addOrderLog(o.getOrderNo(),OrderLogProcess.WBTG.getCode(),o.getRemarks());
				aids.add(departmentMapper.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(), AFTConstants.SALESMAN_ADMIN));
				type=NoticeStatus.ORDER_OUTSOURCE_NO.getCode();
			}
			tOrderNewMapper.update(t);
		}else if (o.getType()==1) {//项目外包
			TOrderTask tt=tOrderTaskMapper.queryById(o.getTid());
			aids.add(tt.getTaskReceiver());
			Admin a =adminMapper.selectById(TokenManager.getAdminId());
			OutsourceLog ol=new OutsourceLog();
			ol.setAid(TokenManager.getAdminId());
			ol.setAname(a.getName());
			ol.setRemarks(o.getRemarks());
			ol.setTid(o.getTid());
			if (o.getRefundStatus()==1) {
				//把项目更改成外包项目
				TOrderTask tt2=new TOrderTask();
				tt2.setId(o.getTid());
				tt2.setOutsource(1);
				tOrderTaskMapper.update(tt2);
				type=NoticeStatus.PROJECT_OUTSOURCE_YES.getCode();
				ol.setStatus(1);
				addOrderLog(o.getOrderNo(),OrderLogProcess.XMWBTG.getCode(),o.getRemarks());

			}else {
				type=NoticeStatus.PROJECT_OUTSOURCE_NO.getCode();
				ol.setStatus(2);
				addOrderLog(o.getOrderNo(),OrderLogProcess.XMWBBH.getCode(),o.getRemarks());
				pushRejectMid(o);
			}
			outsourceLogMapper.insertSelective(ol);
		}
		TOrderNewBo order=tOrderNewMapper.getSaleIdByOno(o.getOrderNo());
		orderNewService.pushGeneralSendNoticeAndEmail(aids, type,order, TokenManager.getAdminId(),t2.getApproval(),o.getTid());
		tOrderOutsourceMapper.updateByPrimaryKeySelective(o);
		if(o.getRefundStatus()==1 &&o.getType()==1){
			OrderNewService.updateOrderCostAmount(o.getTid(),0);
		}
		return 1;
	}

	private void pushRejectMid(OrderOutsourceDtails o) {
		TTaskMid tm=new TTaskMid();
		tm.setTid(o.getTid());
		tm.setCostAmount(new BigDecimal(0));
		tTaskMidMapper.updateByTid(tm);
		tOrderMidMapper.updateCostAmount(o.getOrderNo());
	}

	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;
	}
	@Override
	public List<OutOutsourceLog> selectOutsourceLog(Integer tid) {
		return outsourceLogMapper.selectByTid(tid);
	}
	@Override
	public int updateSuperAdminReject(Integer id) {
		TOrderOutsource tto=tOrderOutsourceMapper.selectByPrimaryKey(id);
		TOrderTask tt=new TOrderTask();
		tt.setId(tto.getTid());
		//start_type 0外包 1供应商
		if (tto.getStartType()==0) {
			tt.setOutsource(0);
		}else {
			tt.setCheckStatus(2);
		}
		tOrderTaskMapper.update(tt);
		OutsourceLog ol=new OutsourceLog();
		ol.setTid(tto.getTid());
		ol.setAid(TokenManager.getAdminId());
		ol.setAname("超级管理员");
		ol.setStatus(2);
		ol.setCreateTime(new Date());
		ol.setRemarks("超级管理员强制驳回");
		outsourceLogMapper.insert(ol);
		TOrderOutsource tto2=new  TOrderOutsource();
		tto2.setId(id);
		tto2.setRefundStatus(2);
		TTaskMid ttm=new TTaskMid();
		ttm.setTid(tto.getTid());
		ttm.setCostAmount(new BigDecimal(0));
		tTaskMidMapper.updateByTid(ttm);
		ProjectTypePuls typePuls = tOrderTaskMapper.selectByTidGetDtails(tto.getTid());
		tOrderMidMapper.updateCostAmount(typePuls.getOrderNo());
		return tOrderOutsourceMapper.updateByPrimaryKeySelective(tto2);
	}

}
