package com.goafanti.order.controller;

import javax.annotation.Resource;

import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.order.bo.OutSelectOrderDun;
import com.goafanti.order.bo.OutSelectOrderDunAll;
import org.springframework.beans.factory.annotation.Value;
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.utils.StringUtils;
import com.goafanti.order.bo.InputNewOrderDunBo;
import com.goafanti.order.service.NewOrderDunService;

import java.util.List;

@RestController
@RequestMapping(value = "/api/admin/newOrderDun")
public class AdminNewOrderDunApiController extends CertifyApiController {

	@Resource
	private NewOrderDunService	newOrderDunService;
	@Value(value = "${upload.path}")
	private String	uploadPath			= null;

	/**
	 * 创建催款信息
	 * @return
	 */
	@RequestMapping(value = "/createDun",method = RequestMethod.POST)
	public Result createDun(InputNewOrderDunBo ib){
		Result res = new Result();
		if(StringUtils.isBlank(ib.getOrderNo())||null==ib.getTid()) {
			res.getError().add(buildError("", "订单编号与项目编号必须指定"));
			return res;
		}
		if(null==ib.getProjectType()||null==ib.getDunType()) {
			res.getError().add(buildError("", "项目分类和催款分类必须指定"));
			return res;
		}
		res.data(newOrderDunService.createDun(ib));
		return res;
	}
	/**
	 * 修改催款信息
	 * @return
	 */
	@RequestMapping(value = "/updateDun",method = RequestMethod.POST)
	public Result updateDun(InputNewOrderDunBo ib){
		Result res = new Result();
		if(null==ib.getId()) {
			res.getError().add(buildError("", "催款ID必须指定"));
			return res;
		}
		if(StringUtils.isBlank(ib.getOrderNo())||null==ib.getTid()) {
			res.getError().add(buildError("", "订单编号与项目编号必须指定"));
			return res;
		}
		if(null==ib.getProjectType()||null==ib.getDunType()) {
			res.getError().add(buildError("", "项目分类和催款分类必须指定"));
			return res;
		}
		res.data(newOrderDunService.updateDun(ib));
		return res;
	}
	/**
	 * 删除催款信息
	 * @return
	 */
	@RequestMapping(value = "/deleteDun",method = RequestMethod.POST)
	public Result deleteDun(Integer id){
		Result res = new Result();
		if(null==id) {
			res.getError().add(buildError("", "催款ID必须指定"));
			return res;
		}
		res.data(newOrderDunService.deleteDun( id));
		return res;
	}
	/**
	 * 	查看催款信息
	 * @return
	 */
	@RequestMapping(value = "/selectDun",method = RequestMethod.GET)
	public Result selectDun(Integer id){
		Result res = new Result();
		if(null==id) {
			res.getError().add(buildError("", "催款ID必须指定"));
			return res;
		}
		res.data(newOrderDunService.selectDun( id));
		return res;
	}
	/**
	 * 	催款信息列表
	 * @return
	 */
	@RequestMapping(value = "/selectListNewOrderDun",method = RequestMethod.GET)
	public Result selectListNewOrderDun(String orderNo){
		Result res = new Result();
		res.data(newOrderDunService.selectListNewOrderDun(orderNo));
		return res;
	}

	/**
	 * 全国应收款统计
	 * @param depId
	 * @param depName
	 * @param startDate
	 * @param endDate
	 * @param sort 0按应收款 1按单量 2按金额
	 * @return
	 */
	@RequestMapping(value = "/selectOrderDunAll",method = RequestMethod.GET)
	public Result selectOrderDunAll(String depId,String depName,String startDate ,String endDate,Integer sort){
		Result res = new Result();
		List<OutSelectOrderDunAll> list =newOrderDunService.selectOrderDunAll(depId,startDate,endDate, sort);
		res.data(list);
		return res;
	}

	/**
	 * 	全国应收款统计 导出
	 * @return
	 */
	@RequestMapping(value = "/selectOrderDunAllExprot",method = RequestMethod.GET)
	public Result selectOrderDunAllExprot(String depId,String depName,String startDate ,String endDate,Integer sort){
		List<OutSelectOrderDunAll> list=newOrderDunService.selectOrderDunAll(depId,startDate,endDate, sort);
		NewExcelUtil<OutSelectOrderDunAll> excelUtil=new NewExcelUtil<>(OutSelectOrderDunAll.class);
		StringBuffer sb=new StringBuffer("搜索条件=》");
		if (depName!=null)sb=sb.append("省份：").append(depName).append(",");
		if (sort!=null){
			sb=sb.append("排序：");
			if (sort==0)sb=sb.append("排序：").append("按应收款").append(",");
			if (sort==1)sb=sb.append("排序：").append("按单量").append(",");
			if (sort==2)sb=sb.append("排序：").append("按金额").append(",");
		}
		if(startDate!=null&&endDate!=null)sb=sb.append("催收时间：").append(startDate).append("~").append(endDate).append(",");
		return excelUtil.exportExcel(list,"签单统计表",uploadPath,sb.substring(0,sb.toString().length()-1));
	}

	/**
	 * 	省份应收款统计详情列表
	 * @return
	 */
	@RequestMapping(value = "/selectOrderDun",method = RequestMethod.GET)
	public Result selectOrderDun(String depId,String depName,String aid,String aName,Integer sort,Integer province,String startDate ,String endDate){
		Result res = new Result();
		res.data(newOrderDunService.selectOrderDun(depId,depName,aid,aName,sort,province,startDate,endDate));
		return res;
	}

	/**
	 * 	催款信息列表
	 * @return
	 */
	@RequestMapping(value = "/selectOrderDunExprot",method = RequestMethod.GET)
	public Result selectOrderDunExprot(String depId,String depName,String aid,String aName,Integer sort,Integer province,String startDate ,String endDate){
		List<OutSelectOrderDun> list=newOrderDunService.selectOrderDun(depId,depName,aid,aName,sort,province,startDate,endDate);
		NewExcelUtil<OutSelectOrderDun> excelUtil=new NewExcelUtil<>(OutSelectOrderDun.class);
		StringBuffer sb=new StringBuffer("搜索条件=》");
		if (depName!=null)sb=sb.append("催收部门：").append(depName).append(",");
		if (aName!=null)sb=sb.append("催款人：").append(aName).append(",");
		if (sort!=null){
			sb=sb.append("排序：");
			if (sort==0)sb=sb.append("排序：").append("按签单额").append(",");
			if (sort==1)sb=sb.append("排序：").append("按应收款").append(",");
		}
		if(startDate!=null&&endDate!=null)sb=sb.append("催收时间：").append(startDate).append("~").append(endDate).append(",");
		return excelUtil.exportExcel(list,"催款统计表",uploadPath,sb.substring(0,sb.toString().length()-1));
	}
}
