package com.goafanti.order.controller;


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

import com.goafanti.order.enums.OrderImgEnums;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.business.service.BusinessProjectService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.OrderListBo;
import com.goafanti.order.service.OrderService;

@RestController
@RequestMapping(value = "/api/admin/order")
public class AdminOrderApiController extends CertifyApiController {
	@Resource
	private OrderService orderServiceImpl;
	@Resource
	private BusinessProjectService businessProjectServiceImpl;
	/**
	 * 我的服务订单
	 * @return
	 */
	@RequestMapping(value="/listServiceOrder" ,method = RequestMethod.POST)
	public Result listServiceOrder(OrderListBo bo,String startDate,String endDate,Integer intention,Integer formal,
			Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.selectServiceOrder(bo, startDate, endDate, intention, formal, pageNo, pageSize));
		return res;
	}










	/**
	 * 模糊查询业务项目
	 * @param businessName
	 * @return
	 */
	@RequestMapping(value = "/getBusinessProjectByName", method = RequestMethod.GET)
	public Result getBusinessProjectByName(String businessName,String cname){
		Result res = new Result();
		res.setData(businessProjectServiceImpl.selectBusinessProjectByName(businessName,cname));
		return res;
	}

	/**
	 * 查询业务项目分类
	 * @return
	 */
	@RequestMapping(value="/getBusinessProjectCatalog",method = RequestMethod.GET)
	public Result getBusinessProjectCatalog(){
		Result res = new Result();
		res.setData(businessProjectServiceImpl.selectBusinessProjectCatalog());
		return res;
	}





	/**
	 * 进入修改商品信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/toUpdateOrderCommodity",method = RequestMethod.GET)
	public Result toUpdateOrderCommodity(String id){
		Result res = new Result();
		if(StringUtils.isBlank(id)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","明细ID"));
			return res;
		}

		return res;
	}





	/**
	 * 查询分管部门
	 * @return
	 */
	@RequestMapping(value = "/selectMyDeps")
	public Result selectMyDeps(){
		Result res = new Result();
			res.setData(orderServiceImpl.selectMyDeps());
		return res;
	}

	/**
	 * 部门服务订单
	 * @return
	 */
	@RequestMapping(value="/listDepServiceOrder" ,method = RequestMethod.POST)
	public Result listDepServiceOrder(OrderListBo bo,String startDate,String endDate,Integer intention,Integer formal,
			String depIds,Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.selectDepServiceOrder(bo, startDate, endDate, intention, formal, depIds, pageNo, pageSize));
		return res;
	}

	/**
	 * 查询特批订单
	 * @return
	 */
	@RequestMapping(value="/listApprovalServiceOrder" ,method = RequestMethod.POST)
	public Result listApprovalServiceOrder(OrderListBo bo,String startDate,String endDate,
			String depIds,Integer approvalHistory,Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.selectApprovalServiceOrder(bo, startDate, endDate, depIds, approvalHistory, pageNo, pageSize));
		return res;
	}





	/** 图片上传 **/
	@RequestMapping(value = "/uploadOrderImg", method = RequestMethod.POST)
	public synchronized Result uploadOrderImg(HttpServletRequest req,String orderNo,String sign){
		Result res = new Result();
		if (StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"订单编号","订单编号"));
			return  res;
		}
		if (!sign.equals(OrderImgEnums.CONTRACT.getCode())&&
				!sign.equals(OrderImgEnums.SUPPLEMENT.getCode())&&
				!sign.equals(OrderImgEnums.CONTENT.getCode())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"sign","sign"));
			return  res;
		}
		String imageUrl=uploadFile(res, "/new_order_file/", req,orderNo, sign);
		orderServiceImpl.pushOrderImg(orderNo,imageUrl,sign);
		res.setData(imageUrl);
		return res;
	}

	@RequestMapping(value = "/deleteOrderImg", method = RequestMethod.POST)
	public Result deleteOrderImg(String fileName,String orderNo,String sign){
		Result res = new Result();
		if (StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"订单编号","订单编号"));
			return  res;
		}
		if (!sign.equals(OrderImgEnums.CONTRACT.getCode())&&
				!sign.equals(OrderImgEnums.SUPPLEMENT.getCode())&&
				!sign.equals(OrderImgEnums.CONTENT.getCode())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"sign","sign"));
			return  res;
		}
		deleteFile(res,fileName);
		res.setData(orderServiceImpl.deleteOrderImg(fileName,orderNo,sign));
		return res;
	}



	/** 查询公共订单 **/
	@RequestMapping(value = "/listPublicOrder", method = RequestMethod.POST)
	public Result listPublicOrder(OrderListBo bo,String startDate,String endDate,Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.selectPublicOrder(bo,startDate,endDate,pageNo,pageSize));
		return res;
	}



	/** 我的结算查询 **/
	@RequestMapping(value = "/listMySettlement", method = RequestMethod.POST)
	public Result listMySettlement(String orderNo, String depId,String buyerName,String salesmanName, String financeName,
			Integer orderStatus,Integer liquidationStatus,Integer approval,String startDate,
			String endDate,Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.selectSettlementList(orderNo, depId, buyerName, salesmanName,
				TokenManager.getAdminId(), financeName, orderStatus, liquidationStatus, approval,null,startDate,endDate, pageNo, pageSize));
		return res;
	}

	/** 结算订单管理 **/
	@RequestMapping(value = "/listAllSettlement", method = RequestMethod.POST)
	public Result listAllSettlement(String orderNo, String depId,String buyerName,String salesmanName, String financeName,
			Integer orderStatus,Integer liquidationStatus,Integer approval,Integer isDistributionFinance,String startDate,String endDate,
			Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.selectSettlementList(orderNo, depId, buyerName, salesmanName,
				"", financeName, orderStatus, liquidationStatus, approval,isDistributionFinance, startDate,endDate, pageNo, pageSize));
		return res;
	}

	/** 我的订单流水管理 **/
	@RequestMapping(value = "/listMyBill", method = RequestMethod.POST)
	public Result selectMyBillList(String payeeName, String payerName, String depId,
			String salesmanName,String orderNo, Integer confirmSign,Integer deleteSign,Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.selectMyBillList(payeeName, payerName, depId, salesmanName,orderNo,
				confirmSign, deleteSign, pageNo, pageSize));
		return res;
	}


	/** 我的订单流水查询 **/
	@RequestMapping(value = "/queryMyBill", method = RequestMethod.POST)
	public Result queryMyBillList(String payeeName, String payerName, String depId,
			String salesmanName,String orderNo, Integer confirmSign,Integer deleteSign,Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.queryMyBillList(payeeName, payerName, depId, salesmanName,orderNo,
				confirmSign, deleteSign, pageNo, pageSize));
		return res;
	}


	/** 所有订单流水查询 **/
	@RequestMapping(value = "/queryAllBill", method = RequestMethod.POST)
	public Result queryAllBill(String payeeName, String payerName, String depId,
			String salesmanName,String financeName,String orderNo, Integer confirmSign,Integer deleteSign, Integer pageNo, Integer pageSize){
		Result res = new Result();
		res.setData(orderServiceImpl.queryAllBillList(payeeName, payerName, depId, salesmanName,financeName, orderNo,confirmSign,deleteSign, pageNo, pageSize));
		return res;
	}



	/**
	 * 修改流水
	 * @param billNo
	 * @param financialPayNo
	 * @param financialPayTime
	 * @param remarks
	 * @return
	 */
	@RequestMapping(value = "/updateBill", method = RequestMethod.POST)
	public Result updateBill(String billNo,String orderNo, String financialPayNo, String financialPayTime, String remarks){
		Result res = new Result();
		if(StringUtils.isBlank(billNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","流水编号"));
			return res;
		}
		if(StringUtils.isBlank(financialPayNo) || StringUtils.isBlank(financialPayTime)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","财务流水号和财务流水时间"));
			return res;
		}
		orderServiceImpl.updateBill(billNo, orderNo,financialPayNo, financialPayTime, remarks);
		return res;
	}





	/**
	 * 查看变更记录
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/viewOrderChange",method = RequestMethod.GET)
	public Result viewOrderChange(String orderNo){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		res.setData(orderServiceImpl.selectOrderChange(orderNo));
		return res;
	}




	/**
	 * 修复订单数据
	 * @return
	 */
	@RequestMapping(value="/repairData",method = RequestMethod.GET)
	public Result repairData(){
		Result res = new Result();
		orderServiceImpl.updateRepairData();
		return res;
	}
	/**
	 * 处理旧项目任务
	 */
	@RequestMapping(value="/addContractTaskLog",method = RequestMethod.GET)
	public Result addContractTaskLog(){
		Result res = new Result();
		orderServiceImpl.addContractTaskLog();
		return res;
	}
}
