package com.goafanti.customer.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goafanti.common.dao.OrganizationAnnualReportMapper;
import com.goafanti.common.dao.OrganizationYearProjectMapper;
import com.goafanti.common.dao.UserMapper;
import com.goafanti.common.model.OrganizationAnnualReport;
import com.goafanti.common.model.OrganizationYearProject;
import com.goafanti.common.model.User;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.customer.bo.CustomerOrganizationDetailBo;
import com.goafanti.customer.service.AdminOrgAnnualReportService;
import com.goafanti.customer.service.CustomerService;
@Service
public class AdminOrgAnnualReportServiceImpl  extends BaseMybatisDao<OrganizationAnnualReportMapper> implements AdminOrgAnnualReportService {
	@Autowired
	private OrganizationAnnualReportMapper	organizationAnnualReportMapper;
	@Autowired
	private OrganizationYearProjectMapper	organizationYearProjectMapper;
	@Autowired
	private UserMapper	userMapper;
	@Autowired
	private CustomerService customerService;
	
	@Override
	public int addOrgAnnual(OrganizationAnnualReport o) {
		o.setId(UUID.randomUUID().toString());
		o.setAid(TokenManager.getAdminId());
//		o.setAid("1");
		 int i=organizationAnnualReportMapper.insertSelective(o);
		countIntegrity(o.getUid());
		return i;
	}

	private void countIntegrity(String uid) {
		User u= new User();
		CustomerOrganizationDetailBo bo=userMapper.findOrganizationCustomerDetail(uid);
		u.setId(uid);
		u.setIntegrity(customerService.countIntegrity(bo));
		userMapper.updateByPrimaryKeySelective(u);
	}

	@Override
	public int delectOrgAnnual(OrganizationAnnualReport o) {
		int i=organizationAnnualReportMapper.deleteByPrimaryKey(o.getId());
		countIntegrity(o.getUid());
		return i; 
	}

	@Override
	public OrganizationAnnualReport selectOrgAnnual(String id) {
		return organizationAnnualReportMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateOrgAnnual(OrganizationAnnualReport o) {
		return organizationAnnualReportMapper.updateByPrimaryKeySelective(o);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrganizationAnnualReport> selectListOrgAnnual(OrganizationAnnualReport o, Integer pNo, Integer pSize) {
		if(null==pNo)pNo=1;
		if(null==pSize)pSize=10;
		Map<String,Object> params = new HashMap<>();
		params.put("uid", o.getUid());
		Pagination<OrganizationAnnualReport> list = (Pagination<OrganizationAnnualReport>) findPage("selectOrgAnnualList","selectOrgAnnualCount",params,pNo,pSize);
		return list;
		
	}
	@Override
	public boolean checkYear(OrganizationAnnualReport o) {
		int i=organizationAnnualReportMapper.checkYear(o.getUid(),o.getYear());
		if(i>0)return true;
		return false;
	}
	@Override
	public boolean checkAnnual(OrganizationAnnualReport o) {
		int i=organizationAnnualReportMapper.checkAnnual(o.getId(),o.getUid(),o.getYear());
		if(i>0)return true;
		return false;
	}


	@Override
	public int addOrgYearProject(OrganizationYearProject o) {
		o.setId(UUID.randomUUID().toString());
		o.setAid(TokenManager.getAdminId());
//		o.setAid("1");
		int i=organizationYearProjectMapper.insertSelective(o);
		countIntegrity(o.getUid());
		return i; 
	}

	@Override
	public int delectOrgYearProject(OrganizationYearProject o) {
		int i=organizationYearProjectMapper.deleteByPrimaryKey(o.getId());
		countIntegrity(o.getUid());
		return i; 
	}

	@Override
	public OrganizationYearProject selectOrgYearProject(String id) {
		return organizationYearProjectMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateOrgYearProject(OrganizationYearProject o) {
		return organizationYearProjectMapper.updateByPrimaryKeySelective(o);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrganizationYearProject> selectListOrgYearProject(OrganizationYearProject o, Integer pNo,
			Integer pSize) {
		if(null==pNo)pNo=1;
		if(null==pSize)pSize=10;
		Map<String,Object> params = new HashMap<>();
		params.put("uid", o.getUid());
		Pagination<OrganizationYearProject> list = (Pagination<OrganizationYearProject>) findPage("selectOrgYearProjectList","selectOrgYearProjectCount",params,pNo,pSize);
		return list;
		
	}


	

}
