package com.goafanti.demandCollection.controller;



import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.DemandCollection;
import com.goafanti.common.model.SkjtUser;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.demandCollection.bo.InputDemandCollection;
import com.goafanti.demandCollection.bo.InputUserList;
import com.goafanti.demandCollection.bo.SkjtConstants;
import com.goafanti.demandCollection.service.DemandCollectionService;

@RestController
@RequestMapping(value="/")
public class DemandCollectionApiController  extends CertifyApiController {
	@Resource
	private DemandCollectionService		demandCollectionService;
	
	
	/**
	 * 新增需求收集
	 * @param d
	 * @return
	 */
	@RequestMapping(value="/open/addDemandCollection",method = RequestMethod.POST)
	public Result addDemandCollection(DemandCollection d){
		Result res =new Result();
		if (StringUtils.isBlank(d.getDwmc())) {
			res.getError().add(buildError("名称必须指定","名称必须指定"));
			return res;
		}
		res.data(demandCollectionService.addDemandCollection(d));
		return res;
		
	}
	/**
	   * 需求收集列表
	 * @param d
	 * rygm 人员规模 0、50人以下   1、50~100人 2、100人以上
	 * jjgm 经济规模 0、300万以下 1、300~500万  2、500~1000万    3、1000万以上
	 * @return
	 */
	@RequestMapping(value="/api/demandCollectionList",method = RequestMethod.GET)
	public Result demandCollectionList(InputDemandCollection i){
		Result res =new Result();
		res.data(demandCollectionService.demandCollectionList(i));
		return res;
		
	}
	/**
	   *模拟登陆
	 * @param d
	 * @return
	 */
	@RequestMapping(value="/open/login",method = RequestMethod.POST)
	public Result demandCollectionList(String userName ,String password){
		Result res =new Result();
		//模拟登陆
		if (userName==null||password==null) {
			res.getError().add(buildError("账号密码不能为空！","账号密码不能为空！"));
			return  res ;
		}
		SkjtUser user=new SkjtUser();
		user.setAccount(userName);
		user.setPassword(password);
		res.setData(TokenManager.skjtLogin(user, true));
		return res;
		
	}
	
	/**
	   *用户列表
	 * @param d
	 * @return
	 */
	@RequestMapping(value="/api/userList",method = RequestMethod.GET)
	public Result userList(InputUserList in){
		Result res =new Result();
		res.setData(demandCollectionService.userList(in));
		return res;
		
	}
	
	/**
	   *修改密码
	 * @param d
	 * @return
	 */
	@RequestMapping(value="/api/updateUserPassword",method = RequestMethod.POST)
	public Result updateUserPassword(String account,String usedPassword,String newPassword){
		Result res =new Result();
		if (StringUtils.isBlank(usedPassword)||StringUtils.isBlank(newPassword)) {
			res.getError().add(buildError("密码不能为空！","密码不能为空！"));
			return res;
		}
		if (TokenManager.hasRole(SkjtConstants.GLY)&&StringUtils.isBlank(account)) {
			res.getError().add(buildError("账号不能为空！","账号不能为空！"));
			return res;
		}
		res.setData(demandCollectionService.updateUserPassword(account,usedPassword,newPassword));
		return res;
		
	}
	/**
	 * 	导出变更列表
     * 
     * @param response
     * @return
     */
    @RequestMapping(value = "/api/exportDemandCollectionList" , method = RequestMethod.GET)
    public Result exportDemandCollectionList(HttpServletResponse response,InputDemandCollection i)  {
    	Result res=new Result();
			try {
				demandCollectionService.exportDemandCollectionList( response, i);
			} catch (Exception e) {
				res.getError().add(buildError("格式不正确"));
				e.printStackTrace();
				return res;
			}
         res.data(1); 
    	return res;
    }
   
}
