package com.goafanti.order.service.impl;

import com.goafanti.admin.service.DepartmentService;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.dao.TOrderBonusMapper;
import com.goafanti.common.dao.TOrderMonthReportMapper;
import com.goafanti.common.dao.TOrderNewMapper;
import com.goafanti.common.model.TOrderBonus;
import com.goafanti.common.model.TOrderNew;
import com.goafanti.common.utils.ExportExcelUtil;
import com.goafanti.common.utils.StringUtils;
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.OrderListBo;
import com.goafanti.order.bo.OrderReportBo;
import com.goafanti.order.bo.TOrderBonusBo;
import com.goafanti.order.service.OrderBonusService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderBonusServiceImpl extends BaseMybatisDao<TOrderBonusMapper> implements OrderBonusService {

	@Autowired
	private TOrderBonusMapper bonusMapper;
	@Autowired
	private TOrderNewMapper orderNewMapper;
	@Autowired
	private AdminMapper	adminMapper;
	@Autowired
	private TOrderMonthReportMapper orderMonthReportMapper;
	@Autowired
	private DepartmentService departmentService;

	@Override
	public Integer addOrderBouns(TOrderBonus bonus) {
		// 生成id
		bonus.setId(UUID.randomUUID().toString());
		return bonusMapper.insertSelective(bonus);
	}

	@Override
	public Integer updateBounsPaymnetStatus(String id,String orderNo) {
		//先确定订单有没有校对信息
		TOrderNew o = orderNewMapper.queryById(orderNo);
		if(null == o || StringUtils.isBlank(o.getProofAid())){//不存在或者没有校对
			return -1;
		}
		TOrderBonus bonus = new TOrderBonus();
		bonus.setId(id);
		bonus.setGrantStatus(1);
		//bonus.setGrantTime(new Date());发放时间预留
		return bonusMapper.updateByPrimaryKeySelective(bonus);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrderListBo> technicianBonusStatistics(TOrderBonusBo newBo,Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		List<String>fids= new ArrayList<>();
		if (TokenManager.hasRole(AFTConstants.FINANCE)) {
			fids.add(TokenManager.getAdminId());
		}else if (TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)) {
			fids=adminMapper.selectBySuperId(TokenManager.getAdminId());
		}
		params.put("fids", fids);
		params.put("b", newBo);
		Pagination<OrderListBo> data = (Pagination<OrderListBo>)findPage("technicianBonusList", "technicianBonusCount", params, pageNo, pageSize);
		return data;
	}



	@SuppressWarnings("unchecked")
	@Override
	public Pagination<TOrderBonusBo> saleBonusStatistics(TOrderBonusBo newBo, Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();

		List<String> fids=new ArrayList<>();
		if (TokenManager.hasRole(AFTConstants.FINANCE)) {
			newBo.setGrantBy(TokenManager.getAdminId());
		}else if (TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)) {
			fids=adminMapper.selectBySuperId(TokenManager.getAdminId());
		}
		List<String> depsList = new ArrayList<>();
		if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)){
			depsList=departmentService.selectMyDeps();
		}
		if (newBo.getDeps()==null){
			if (depsList.isEmpty()){
				return new Pagination<>(1,10,0);
			}
			params.put("deps", depsList);
		}else {
			depsList=departmentService.comparePermissions(depsList, newBo.getDeps());
			if (depsList.isEmpty()){
				return new Pagination<>(1,10,0);
			}
			params.put("deps", depsList);
		}
		params.put("fids", fids);
		params.put("b", newBo);
		Pagination<TOrderBonusBo> data = (Pagination<TOrderBonusBo>)findPage("saleBonusList", "saleBonusCount", params, pageNo, pageSize);

		innerData((List<TOrderBonusBo>) data.getList(), newBo);
		return data;
	}

	private void innerData(List<TOrderBonusBo> list, TOrderBonusBo newBo){
		Integer m = null;
		Integer m1 = null;
		Integer y = null;
		Integer y1 = null;
		if(StringUtils.isNotEmpty(newBo.getStartTime()) && StringUtils.isNotEmpty(newBo.getEndTime())){
			m = Integer.valueOf(newBo.getStartTime().replaceAll("-", ""));
			m1 = Integer.valueOf(newBo.getEndTime().replaceAll("-", ""));
			y = Integer.valueOf(newBo.getStartTime().substring(0, newBo.getStartTime().indexOf("-")));
			y1 = Integer.valueOf(newBo.getEndTime().substring(0, newBo.getEndTime().indexOf("-")));
		}else{
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			y = cal.get(Calendar.YEAR);
			m = y*100 + (cal.get(Calendar.MONTH)+1);
			y1 = y;
			m1 = m;
		}
		for (TOrderBonusBo orderListBo : list) {
			//处理月度数据
			OrderReportBo r = new OrderReportBo();
			r.setSalemansId(orderListBo.getGrantTarget());
			r.setMonth(m);
			r.setMonth1(m1);
			List<OrderReportBo> l = orderMonthReportMapper.getMonthReportByMonth(r);
			if(l.size() == 1){
				OrderReportBo o = l.get(0);
				if(null != o){
					orderListBo.setKejim(o.getKejim());
					orderListBo.setRenzm(o.getRenzm());
				}
			}

			r.setMonth(y);
			r.setMonth1(y1);
			//处理年度数据
			List<OrderReportBo> ll = orderMonthReportMapper.getYearReportByYear(r);
			if(l.size() == 1){
				OrderReportBo o = ll.get(0);
				if(null != o){
					orderListBo.setKejiy(o.getKejiy());
					orderListBo.setRenzy(o.getRenzy());
				}
			}
		}
	}

	@Override
	public Integer updateChangeBonus(String id) {
		TOrderBonus bonus = new TOrderBonus();
		bonus.setId(id);
		bonus.setGrantStatus(1);
		return bonusMapper.updateByPrimaryKeySelective(bonus);
	}

	@Override
	public XSSFWorkbook exportTechnicianBonusData(TOrderBonusBo bonusBo) {
		// 判断导出是否有部门
		String deptid = bonusBo.getDeps();
		Map<String, List<TOrderBonusBo>> map = new HashMap<String, List<TOrderBonusBo>>();
		if(StringUtils.isBlank(deptid)){
			//获得所有有数据的部门
			bonusBo.setGrantType(1);
			List<TOrderBonusBo> dlist = bonusMapper.getDeptBySome(bonusBo);
			if(null == dlist || dlist.size() == 0){
				return null;
			}
			for (int i = 0; i < dlist.size(); i++) {
				TOrderBonusBo ob = dlist.get(i);
				if(null != ob && null!= ob.getDeps()){
//					bonusBo.setDepartmentId(ob.getDepartmentId());
					map.put(ob.getDepartmentName(), bonusMapper.getBonusData(bonusBo));
				}
			}
		}else{
			map.put(bonusBo.getDepartmentName(), bonusMapper.getBonusData(bonusBo));
		}
		String[] comment = {"任务编号","任务名称","订单编号","任务负责人","任务状态","任务数量","项目状态","受理日期","评审日期","下证日期","订单结算状态","发放类型","发放状态","奖金创建时间"};
		XSSFWorkbook wb = ExportExcelUtil.exportDeptBonusInfoT(map, comment);
		return wb;
	}

	@Override
	public XSSFWorkbook exportSaleBonusData(TOrderBonusBo bonusBo) {
		String deptid = bonusBo.getDeps();
		Map<String, List<TOrderBonusBo>> map = new HashMap<String, List<TOrderBonusBo>>();
		if(StringUtils.isBlank(deptid)){
			//获得所有有数据的部门
			bonusBo.setGrantType(0);
			List<TOrderBonusBo> dlist = bonusMapper.getDeptBySome(bonusBo);
			if(null == dlist || dlist.size() == 0){
				return null;
			}
			for (int i = 0; i < dlist.size(); i++) {
				TOrderBonusBo ob = dlist.get(i);
//				bonusBo.setDepartmentId(ob.getDepartmentId());
				List<TOrderBonusBo> l =  bonusMapper.getSaleBounsList(bonusBo);
				innerData(l, bonusBo);
				map.put(ob.getDepartmentName(), l);
			}
		}else{
			List<TOrderBonusBo> l =  bonusMapper.getSaleBounsList(bonusBo);
			innerData(l, bonusBo);
			map.put(bonusBo.getDepartmentName(), l);
		}
		XSSFWorkbook wb = ExportExcelUtil.exportDeptBonusInfoS(map);
		return wb;
	}

}
