package com.goafanti.order.controller;


import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.TOrderInvoice;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.order.bo.InputOrderInvoiceBo;
import com.goafanti.order.bo.TOrderInvoiceBo;
import com.goafanti.order.service.OrderInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

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

	@Autowired
	private OrderInvoiceService		orderInvoiceService;
	@Value(value = "${upload.path}")
	private final String	uploadPath			= null;

	/**
	 * 创建发票申请
	 * @return
	 */
	@RequestMapping(value = "/createOrderInvoice",method = RequestMethod.POST)
	public Result createOrderInvoice(TOrderInvoice t){
		Result res = new Result();
		if(t==null||null==t.getInvoiceType()||StringUtils.isBlank(t.getTaxNumber())||
				null==t.getAmount()||null==t.getBanks()||StringUtils.isBlank(t.getContent())
				||StringUtils.isBlank(t.getUnitAddress())||StringUtils.isBlank(t.getUnitMobile())
				||StringUtils.isBlank(t.getUnitName())||StringUtils.isBlank(t.getVoucherUrl())
				||StringUtils.isBlank(t.getOrderNo())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","基本资料"));
			return res;
		}
		//status 0保存 1提交
		if(orderInvoiceService.checkAmount(t)) {
			res.getError().add(buildError("","可开票余额不足!请注意已发起和被驳回的开票信息。"));
			return res;
		}
		res.data(orderInvoiceService.createServiceOrder(t));
		return res;
	}


	/**
	   *   发票申请详情
	 * @return
	 */
	@RequestMapping(value = "/selectByIdOrderInvoice",method = RequestMethod.GET)
	public Result selectByIdOrderInvoice(Integer id){
		Result res = new Result();
		if(id==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","ID"));
			return res;
		}

		res.data(orderInvoiceService.selectByIdOrderInvoice(id));
		return res;
	}
	/**
	 * 获取最新开票详情
	 * @return
	 */
	@RequestMapping(value = "/getInvoiceDetails",method = RequestMethod.GET)
	public Result getInvoiceDetails(String orderNo){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}

		res.data(orderInvoiceService.getInvoiceDetails(orderNo));
		return res;
	}
	/**
	 * 修改发票申请
	 * @return
	 */
	@RequestMapping(value = "/updateOrderInvoice",method = RequestMethod.POST)
	public Result updateOrderInvoice(TOrderInvoice t){
		Result res = new Result();
		if(t==null||null==t.getInvoiceType()||StringUtils.isBlank(t.getTaxNumber())||
				null==t.getAmount()||null==t.getBanks()||StringUtils.isBlank(t.getContent())
				||StringUtils.isBlank(t.getUnitAddress())||StringUtils.isBlank(t.getUnitMobile())
				||StringUtils.isBlank(t.getUnitName())||StringUtils.isBlank(t.getVoucherUrl())
				||StringUtils.isBlank(t.getOrderNo())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","基本资料"));
			return res;
		}
		if(orderInvoiceService.checkAmount(t)) {
			res.getError().add(buildError("","可开票余额不足!"));
			return res;
		}
		res.data(orderInvoiceService.updateServiceOrder(t));
		return res;
	}


	/**
	 *  财务开票列表
	 * @return
	 */
	@RequestMapping(value = "/financeOrderInvoiceList",method = RequestMethod.GET)
	public Result financeOrderInvoiceList(InputOrderInvoiceBo o,Integer pageNo,Integer pageSize){
		Result res = new Result();

		res.data(orderInvoiceService.financeOrderInvoiceList( o,pageNo,pageSize));
		return res;
	}

	/**
	 *  财务开票列表
	 * @return
	 */
	@RequestMapping(value = "/financeOrderInvoiceList/export",method = RequestMethod.GET)
	public Result financeOrderInvoiceListExport(InputOrderInvoiceBo o,Integer pageNo,Integer pageSize){
		NewExcelUtil<TOrderInvoiceBo> excelUtil=new NewExcelUtil<>(TOrderInvoiceBo.class);
		List<TOrderInvoiceBo> list = (List<TOrderInvoiceBo>) orderInvoiceService.financeOrderInvoiceList(o, pageNo, 99999).getList();
		String exportName=o.getType()==0?"省内开票查询列表":"省外开票查询列表";
		return excelUtil.exportExcel(list,exportName,uploadPath);
	}



	/**
	 *  开票特批列表
	 * @return
	 */
	@RequestMapping(value = "/approvalOrderInvoiceList",method = RequestMethod.GET)
	public Result approvalOrderInvoiceList(Integer status,Integer approval,Integer type,String orderDep,String orderNo ,
										   String name,String userName,Integer pageNo,Integer pageSize){
		Result res = new Result();
		res.data(orderInvoiceService.approvalOrderInvoiceList( status,approval,type, orderDep, orderNo, name,userName,pageNo,pageSize));
		return res;
	}

	/**
	 *   开票审核
	 * @return
	 */
	@RequestMapping(value = "/financeExamine",method = RequestMethod.POST)
	public Result financeExamine(Integer id,Integer examine,String reason){
		Result res = new Result();
		if(id==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","ID"));
			return res;
		}
		//examine 2通过 3拒绝
		if (examine==3&&StringUtils.isBlank(reason)) {
			res.getError().add(buildError("","拒绝必须填写原因。"));
			return res;
		}
		res.data(orderInvoiceService.updatefinanceExamine( id, examine, reason));
		return res;
	}

	/**
	 *   特批审核
	 * @return
	 */
	@RequestMapping(value = "/approvalExamine",method = RequestMethod.POST)
	public Result approvalExamine(Integer id,Integer examine,String reason){
		Result res = new Result();
		if(id==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","ID"));
			return res;
		}
		//examine 2通过 3拒绝
		if (examine==3&&StringUtils.isBlank(reason)) {
			res.getError().add(buildError("","拒绝必须填写原因。"));
			return res;
		}
		res.data(orderInvoiceService.updateapprovalExamine( id, examine, reason));
		return res;
	}

	/**
	 *   订单开票列表
	 * @return
	 */
	@RequestMapping(value = "/salesmanOrderInvoiceList",method = RequestMethod.GET)
	public Result salesmanOrderInvoiceList(String orderNo,Integer pageNo,Integer pageSize){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		res.data(orderInvoiceService.salesmanOrderInvoiceList(  orderNo, pageNo,pageSize));
		return res;
	}

	/** 开票文件上传 **/
	@RequestMapping(value = "/uploadOrderInvoiceFile", method = RequestMethod.POST)
	public Result uploadOrderInvoiceFile(HttpServletRequest req,String sign){
		Result res = new Result();
		//order_invoice_file
		res.setData(handleFile(res, "/order_invoice_file/", false, req, sign));
		return res;
	}

	@RequestMapping(value = "/invoice/log",method = RequestMethod.GET)
	public Result InvoiceLog(Integer id){
		Result res = new Result();
		res .data(orderInvoiceService.InvoiceLog(id));
		return res;
	}

	/**
	 * 删除开票申请
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	public Result delete(Integer id) {
		Result res = new Result();
		if (orderInvoiceService.checkDelete(id)){
			res.getError().add(buildError("该申请状态，无法删除。"));
			return res;
		}
		res.data(orderInvoiceService.delete(id));
		return res;
	}

}
