package com.goafanti.organization.controller;



import java.io.IOException;
import java.math.BigDecimal;

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

import org.springframework.validation.BindingResult;
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.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.model.OfficialFeePrice;
import com.goafanti.common.model.PatentPrice;
import com.goafanti.common.model.SoftWritingPrice;
import com.goafanti.common.model.TOrderPayment;
import com.goafanti.common.model.ThirdPartyCompany;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.organization.bo.InputNodeList;
import com.goafanti.organization.bo.InputPaymentList;
import com.goafanti.organization.bo.InputPaymentNode;
import com.goafanti.organization.bo.InuptFinancialPayment;
import com.goafanti.organization.bo.ThirdPartyError;
import com.goafanti.organization.service.ThirdPartyCompanyService;



@RestController
@RequestMapping("/api/admin/company")
public class ThirdPartyCompanyApiController extends BaseApiController{
	
	@Resource
	private ThirdPartyCompanyService thirdPartyCompanyService;
	
	/**
	 * 新增第三方机构
	 * @param t
	 * @param calculation 0 其他 1软著 2专利
	 * @param patentType 专利名称 0实用新型 1发明专利 2外观专利 3 其他
	 * @param startType  0外包 1供应商  外包全为输入，供应商才分软著和专利从后台选取
	 * @return
	 */
	@RequestMapping(value = "/addCompany" , method = RequestMethod.POST)
	public Result addCompany(@Valid ThirdPartyCompany t,BindingResult bindingResult,
			Integer calculation) {
		Result res =new Result();
		if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
            		ThirdPartyError.getValueByCode(bindingResult.getFieldError().getField())));
            return res;
        }
		if (StringUtils.isBlank(t.getCompanyName())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"名称","名称"));
			return res;
		}
			if (calculation==1) {
				thirdPartyCompanyService.calculationUnitPrice(t);
				
			}else if (calculation==2) {
				thirdPartyCompanyService.calculationPatentUnitPrice(t);
			}
		if(calculation!=0&&(t.getUnitPrice()==null||t.getUnitPrice().compareTo(new BigDecimal(0))<1)) {
			res.getError().add(buildError("后台无此项数据记录，请联系管理员","后台无此项数据记录，请联系管理员"));
			return res;
		}
		if (t.getUnitPrice()==null||t.getTotalAmount()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"单价","单价"));
			return res;
		}
		if (!res.getError().isEmpty()) {
            return res;
        }
		res.data(thirdPartyCompanyService.addCompany(t,calculation));
		return res;
	}
	
	
	/**
	 * 修改第三方机构
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/updateCompany" , method = RequestMethod.POST)
	public Result updateCompany(@Valid ThirdPartyCompany t,BindingResult bindingResult,
			Integer calculation) {
		Result res =new Result();
		if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
            		ThirdPartyError.getValueByCode(bindingResult.getFieldError().getField())));
            return res;
        }
		if (null==t.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		if (StringUtils.isBlank(t.getCompanyName())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"名称","名称"));
			return res;
		}
		//0 系统 1软著 2专利
		if (calculation==1) {
			thirdPartyCompanyService.calculationUnitPrice(t);
		}else if (calculation==2) {
			thirdPartyCompanyService.calculationPatentUnitPrice(t);
		}
		if (!res.getError().isEmpty()) {
            return res;
        }
		res.data(thirdPartyCompanyService.updateCompany(t,calculation));
		return res;
	}
	
	/**
	 * 删除第三方机构
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/deleteCompany" , method = RequestMethod.POST)
	public Result deleteCompany(String id) {
		Result res =new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		res.data(thirdPartyCompanyService.deleteCompany(id));
		return res;
	}
	/**
	 * 第三方机构列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/selectCompany" , method = RequestMethod.GET)
	public Result selectCompany(Integer tid) {
		Result res =new Result();
		if (null==tid) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		res.data(thirdPartyCompanyService.selectCompany(tid));
		return res;
	}
	
	
	/**
	 * 模糊查询机构列表
	 * @param type 类型 0软著 1其他 2专利
	 * @return
	 */
	@RequestMapping(value = "/selectVague" , method = RequestMethod.GET)
	public Result selectVague(String  name,Integer type) {
		Result res =new Result();
		res.data(thirdPartyCompanyService.selectVague(name,type));
		return res;
	}
	
	/**
	 * 新增付款节点
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/addPaymentNode" , method = RequestMethod.POST)
	public Result addPaymentNode(@Valid InputPaymentNode p,BindingResult bindingResult) {
		Result res =new Result();
		if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
            		ThirdPartyError.getValueByCode(bindingResult.getFieldError().getField())));
            return res;
        }
		if (null==p.getTid()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"项目编号","项目编号"));
			return res;
		}
		if (thirdPartyCompanyService.addNodeCheck(p)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"数量或者金额","数量或者金额"));
			return res;
		}
		if (!res.getError().isEmpty()) {
            return res;
        }
		res.data(thirdPartyCompanyService.addPaymentNode(p));
		return res;
	}
	
	/**
	 * 删除付款节点
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/deletePaymentNode" , method = RequestMethod.POST)
	public Result deletePaymentNode(String id) {
		Result res =new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		res.data(thirdPartyCompanyService.deletePaymentNode(id));
		return res;
	}
	/**
	 * 修改付款节点
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/updatePaymentNode" , method = RequestMethod.POST)
	public Result updatePaymentNode(@Valid InputPaymentNode p,BindingResult bindingResult) {
		Result res =new Result();
		if (bindingResult.hasErrors()) {
			System.out.println(ThirdPartyError.getValueByCode(bindingResult.getFieldError().getField()));
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
            		ThirdPartyError.getValueByCode(bindingResult.getFieldError().getField())));
            return res;
        }
		if (null==p.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		if (null==p.getTid()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"项目编号","项目编号"));
			return res;
		}
		if (thirdPartyCompanyService.addNodeCheck(p)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"数量或者金额","数量或者金额"));
			return res;
		}
		if (!res.getError().isEmpty()) {
            return res;
        }
		res.data(thirdPartyCompanyService.updatePaymentNode(p));
		return res;
	}
	
	/**
	 * 付款节点列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/selectPaymentNode" , method = RequestMethod.GET)
	public Result selectPaymentNode(Integer tid) {
		Result res =new Result();
		if (null==tid) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		res.data(thirdPartyCompanyService.selectPaymentNode(tid));
		return res;
	}
	
	/**
	 * 新增付款
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/addOrderPayment" , method = RequestMethod.POST)
	public Result addOrderPayment(TOrderPayment p,BindingResult bindingResult) {
		Result res =new Result();
		if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
            		ThirdPartyError.getValueByCode(bindingResult.getFieldError().getField())));
            return res;
        }
		thirdPartyCompanyService.checkGetAmount(p);
		if (null==p.getTid()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		if (null==p.getChooseType()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"类型","类型"));
			return res;
		}
		if (p.getChooseType()==1&&thirdPartyCompanyService.checkprojectDun(p)) {
			res.getError().add(buildError("项目节点未达到可付款状态。","项目节点未达到可付款状态。"));
			return res;
		}
		if (thirdPartyCompanyService.checkPayment(p)) {
			res.getError().add(buildError("金额超出限制，请核对可输入余额。","金额超出限制，请核对可输入余额。"));
			return res;
		}
		if (!res.getError().isEmpty()) {
            return res;
        }
		res.data(thirdPartyCompanyService.addOrderPayment(p));
		return res;
	}
	/**
	 * 修改付款
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/updateOrderPayment" , method = RequestMethod.POST)
	public Result updateOrderPayment(TOrderPayment p,BindingResult bindingResult) {
		Result res =new Result();
		if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
            		ThirdPartyError.getValueByCode(bindingResult.getFieldError().getField())));
            return res;
        }
		if (null==p.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		if (!res.getError().isEmpty()) {
            return res;
        }
		res.data(thirdPartyCompanyService.updateOrderPayment(p));
		return res;
	}
	
	/**
	 * 付款详情
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/OrderPaymentDetails" , method = RequestMethod.GET)
	public Result OrderPaymentDetails(Integer id) {
		Result res =new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		res.data(thirdPartyCompanyService.OrderPaymentDetails(id));
		return res;
	}
	
	/**
	 * 付款列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/selectOrderPayment" , method = RequestMethod.GET)
	public Result selectOrderPayment(Integer id) {
		Result res =new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		res.data(thirdPartyCompanyService.selectOrderPayment(id));
		return res;
	}
	
	/**
	 * 付款日志
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/selectPaymentLog" , method = RequestMethod.GET)
	public Result selectPaymentLog(Integer id) {
		Result res =new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		res.data(thirdPartyCompanyService.selectPaymentLog(id));
		return res;
	}
	/**
	 * 新增财务付款
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/addfinancialPayment" , method = RequestMethod.POST)
	public Result addfinancialPayment(InuptFinancialPayment f,BindingResult bindingResult) {
		Result res =new Result();
		if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
            		ThirdPartyError.getValueByCode(bindingResult.getFieldError().getField())));
            return res;
        }
		if (null==f.getPartyAmount()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"金额","金额"));
			return res;
		}
		if (null==f.getPaymentTimes()) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"付款时间","付款时间"));
			return res;
		}
		res.data(thirdPartyCompanyService.addfinancialPayment(f));
		return res;
	}
	
	/**
	 * 删除财务付款
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/deleteFinancialPayment" , method = RequestMethod.POST)
	public Result deleteFinancialPayment(Integer id) {
		Result res =new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		res.data(thirdPartyCompanyService.deleteFinancialPayment(id));
		return res;
	}
	/**
	 * 财务付款列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/selectfinancialPayment" , method = RequestMethod.GET)
	public Result selectfinancialPayment(Integer id) {
		Result res =new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
			return res;
		}
		res.data(thirdPartyCompanyService.selectfinancialPayment(id));
		return res;
	}
	
	/**
	 * 付款列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/selectPaymentList" , method = RequestMethod.GET)
	public Result selectPaymentList(InputPaymentList  i) {
		Result res =new Result();
		res.data(thirdPartyCompanyService.selectPaymentList(i));
		return res;
	}
	
	/**
	 * 节点应收款列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/selectNodeList" , method = RequestMethod.GET)
	public Result selectNodeList(InputNodeList  i) {
		Result res =new Result();
		res.data(thirdPartyCompanyService.selectPaymentList(i));
		return res;
	}
	
	/**
	 *  新增软著价格表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/addSoftWritingPrice" , method = RequestMethod.POST)
	public Result addSoftWritingPrice(SoftWritingPrice  s) {
		Result res =new Result();
		if ( s.getMaterial()==null || s.getUrgent()==null || s.getUnitPrice()==null||StringUtils.isBlank(s.getCompanyName())) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
				return res;
		}
		s.setCid(thirdPartyCompanyService.pushSelectByCid(s.getCompanyName(),0));
		if (thirdPartyCompanyService.checkSoftWritingPrice(s)) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR,"此规格","此规格"));
			return res;
		}
		res.data(thirdPartyCompanyService.insertSoftWritingPrice(s));
		return res;
	}
	

	/**
	 *  修改软著价格表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/updateSoftWritingPrice" , method = RequestMethod.POST)
	public Result updateSoftWritingPrice(SoftWritingPrice  s) {
		Result res =new Result();
		if (s.getId()==null) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
				return res;
		}
		if(s.getCompanyName()!=null)s.setCid(thirdPartyCompanyService.pushSelectByCid(s.getCompanyName(),0));
		if (thirdPartyCompanyService.checkSoftWritingPrice(s)) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR,"此规格","此规格"));
			return res;
		}
		res.data(thirdPartyCompanyService.updateSoftWritingPrice(s));
		return res;
	}
	/**
	 *  软著价格表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/listSoftWritingPrice" , method = RequestMethod.GET)
	public Result listSoftWritingPrice(SoftWritingPrice  s,Integer pageNo,Integer pageSize) {
		Result res =new Result();
		if (pageNo==null||pageNo<1) {
			pageNo=1;
		}
		if (pageSize==null||pageSize<1) {
			pageSize=10;
		}
		res.data(thirdPartyCompanyService.listSoftWritingPrice(s,pageNo, pageSize));
		return res;
	}
	/**
	 *  删除软著价格表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/deleteSoftWritingPrice" , method = RequestMethod.POST)
	public Result deleteSoftWritingPrice(Integer id) {
		Result res =new Result();
		if (id==null) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
				return res;
		}
		res.data(thirdPartyCompanyService.deleteSoftWritingPrice(id));
		return res;
	}
	
	/**
	 *  批量确认付款
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/groupPayment" , method = RequestMethod.POST)
	public Result groupPayment(String ids) {
		Result res =new Result();
		if (StringUtils.isBlank(ids)) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
				return res;
		}
		res.data(thirdPartyCompanyService.updateGroupPayment(ids));
		return res;
	}
	
	/**
	 *  	新增官费信息
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/addOfficialFeePrice" , method = RequestMethod.POST)
	public Result addOfficialFeePrice(OfficialFeePrice  o) {
		Result res =new Result();
		if (o.getType()==null||o.getProportion85()==null||
			o.getAmount()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"",""));
			return res;
		}
		res.data(thirdPartyCompanyService.addOfficialFeePrice(o));
		return res;
	}
	

	/**
	 *  	修改官费信息
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/updateOfficialFeePrice" , method = RequestMethod.POST)
	public Result updateOfficialFeePrice(OfficialFeePrice  o) {
		Result res =new Result();
		if (o.getId()==null) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
				return res;
		}
		if (o.getType()==null||o.getProportion85()==null||
				o.getAmount()==null) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"",""));
				return res;
		}
		
		res.data(thirdPartyCompanyService.updateOfficialFeePrice(o));
		return res;
	}
	/**
	 *  删除官费信息
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/deleteOfficialFeePrice" , method = RequestMethod.POST)
	public Result deleteOfficialFeePrice(Integer  id) {
		Result res =new Result();
		if (id==null) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
				return res;
		}
		
		
		res.data(thirdPartyCompanyService.deleteOfficialFeePrice(id));
		return res;
	}
	
	/**
	 * 	 官费列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/listOfficialFeePrice" , method = RequestMethod.GET)
	public Result listOfficialFeePrice(String name) {
		Result res =new Result();
		res.data(thirdPartyCompanyService.listOfficialFeePrice(name));
		return res;
	}
	
	
	
	/**
	 *  	新增专利信息
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/addPatentPrice" , method = RequestMethod.POST)
	public Result addPatentPrice(PatentPrice  p) {
		Result res =new Result();
		if (p.getInventionPatent()==null||p.getInventionPatent()==null||p.getInventionPatent()==null) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"",""));
				return res;
		}
		if(p.getCompanyName()!=null)p.setCid(thirdPartyCompanyService.pushSelectByCid(p.getCompanyName(),2));
		res.data(thirdPartyCompanyService.addpatentPrice(p));
		return res;
	}
	

	/**
	 *  	修改专利信息
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/updatePatentPrice" , method = RequestMethod.POST)
	public Result updatePatentPrice(PatentPrice  p) {
		Result res =new Result();
		if(p.getId()==null) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
				return res;
		}
		if (p.getInventionPatent()==null||p.getInventionPatent()==null||p.getInventionPatent()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"",""));
			return res;
	}
	if(p.getCompanyName()!=null)p.setCid(thirdPartyCompanyService.pushSelectByCid(p.getCompanyName(),2));
		
		res.data(thirdPartyCompanyService.updatepatentPrice(p));
		return res;
	}
	/**
	 *  删除专利信息
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/deletePatentPrice" , method = RequestMethod.POST)
	public Result deletePatentPrice(Integer  id) {
		Result res =new Result();
		if (id==null) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"编号","编号"));
				return res;
		}
		
		
		res.data(thirdPartyCompanyService.deletepatentPrice(id));
		return res;
	}
	
	/**
	 * 	 专利列表
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "/listPatentPrice" , method = RequestMethod.GET)
	public Result listPatentPrice(String name) {
		Result res =new Result();
		res.data(thirdPartyCompanyService.listpatentPrice(name));
		return res;
	}
	
	
	/**
	 * 	导出付款审核表
	 * @throws IOException 
	 */
	@RequestMapping(value = "/exportPaymentList", method = RequestMethod.GET)
	public Result exportPaymentList(HttpServletResponse response,InputPaymentList ip) throws IOException{
		Result res=new Result();
		try {
			thirdPartyCompanyService.exportPaymentList( response, ip);
		} catch (Exception e) {
			res.getError().add(buildError("格式不正确"));
			e.printStackTrace();
			return res;
		}
     res.data(1); 
	return res;
		
	}
	
}
