package com.goafanti.patent.service.impl;


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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.PatentNewLogMapper;
import com.goafanti.common.dao.PatentNewMapper;
import com.goafanti.common.model.PatentNew;
import com.goafanti.common.model.PatentNewLog;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.patent.bo.PatentNewBo;
import com.goafanti.patent.service.PatentNewService;
@Service
public class PatentNewServiceImpl  extends BaseMybatisDao<PatentNewMapper> implements PatentNewService {
	@Autowired
	private PatentNewMapper patentNewMapper;
	@Autowired
	private PatentNewLogMapper patentNewLogMapper;

	@Override
	public int addPatenNew(PatentNew p) {
		p.setStatus(0);//
		p.setAid(TokenManager.getAdminId());
		return patentNewMapper.insertSelective(p);
	}

	@Override
	public int updatePatenNew(PatentNew p) {
		return patentNewMapper.updateByPrimaryKeySelective(p);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<PatentNewBo> selectPatentNew(PatentNewBo p,Integer pageSize, Integer pageNo) {
		Map<String, Object> params = new HashMap<>();
		if (pageNo == null || pageNo < 0) pageNo = 1;
		if (pageSize == null || pageSize < 0)	pageSize = 10;
		if(p.getStatus()!=null)params.put("status", p.getStatus());
		if(p.getName()!=null)params.put("name", p.getName());
		if(p.getAname()!=null)params.put("aname", p.getAname());
		if(p.getPatentNo()!=null)params.put("patentNo", p.getPatentNo());
		if(p.getDepartmentId()!=null)params.put("departmentId", p.getDepartmentId());
		if(p.getStartDate()!=null)params.put("startDate", p.getStartDate());
		if(p.getEndDate()!=null)params.put("endDate", p.getEndDate());
		if(p.getType()!=null)params.put("type", p.getType());
		params.put("aid", TokenManager.getAdminId());
		if (TokenManager.hasRole(AFTConstants.SUPERADMIN) || TokenManager.hasRole(AFTConstants.PATENT_AUDITOR)) {
			params.put("shiro", 2);
		} else  if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)) {
			params.put("shiro", 1);
		} else   {
			params.put("shiro", 0);
		}
		System.out.println(params.get("shiro"));
		return (Pagination<PatentNewBo>) findPage("selectPatentNewList", "selectPatentNewCount", params, pageNo,
				pageSize);
	}

	@Override
	public List<PatentNew> AllselectStartPatentNew() {
		return patentNewMapper.AllselectStartPatentNew();
	}

	@Override
	public int addPatenNewLog(PatentNewLog pl) {
		return patentNewLogMapper.insertSelective(pl);
	}

	@Override
	public List<PatentNew> AllselectEndPatentNew() {
		return patentNewMapper.AllselectEndPatentNew();
	}

	
	
	
}
