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.OrderRefundInvoice;
import com.goafanti.common.model.TChangeDun;
import com.goafanti.common.model.TChangeTask;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.order.bo.*;
import com.goafanti.order.service.OrderChangeService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

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

	@Resource
	private OrderChangeService	orderChangeService;

	/**
	 *  新增变更、发起变更
	 */
	@RequestMapping(value = "/addOrderChange", method = RequestMethod.POST)
	public Result addOrderChange(NewOrderChangeBo t,String startRemarks){
		Result res = new Result();
		if(null==t.getOrderNo()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单编号"));
			return res;
		}
		if(orderChangeService.checkOderNo(t.getOrderNo())){
			res.getError().add(buildError( "", "订单变更未完成"));
			return res;
		}
		res.setData(orderChangeService.addOrderChange(t,startRemarks));
		return res;
	}

	/**
	 * 新增变更项目
	 */
	@RequestMapping(value = "/addChangeTask", method = RequestMethod.POST)
	public Result addChangeTask(@RequestBody InputTChangeTaskBo t){
		Result res = new Result();
		if(StringUtils.isBlank(t.getCommodityId())){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "项目"));
			return res;
		}
		if(null==t.getCommodityPrice()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "项目金额"));
			return res;
		}
		if (orderChangeService.checkTaskMain(t)){
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "", "主项目"));
			return res;
		}
		if(null==t.getCommodityQuantity()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "项目数量"));
			return res;
		}
		if(StringUtils.isBlank(t.getOrderNo())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		if(null==t.getCid()){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更编号"));
			return res;
		}
		res.setData(orderChangeService.addChangeTask(t));
		return res;
	}

	/**
	 *  变更项目列表
	 */
	@RequestMapping(value = "/selectChangeTask", method = RequestMethod.GET)
	public Result selectChangeTask(Integer id){
		Result res = new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更项目id"));
			return res;
		}
		res.setData(orderChangeService.selectChangeTask(id));
		return res;
	}

	/**
	 *  变更催款列表
	 */
	@RequestMapping(value = "/selectChangeDun", method = RequestMethod.GET)
	public Result selectChangeDun(Integer id){
		Result res = new Result();
		if (null==id) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更项目id"));
			return res;
		}
		res.setData(orderChangeService.selectChangeDun(id));
		return res;
	}

	/**
	 *  删除变更项目
	 */
	@RequestMapping(value = "/deleteChangeTask", method = RequestMethod.POST)
	public Result deleteChangeTask(TChangeTask t){
		Result res = new Result();
		if (null==t.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更项目id"));
			return res;
		}
		res.setData(orderChangeService.deleteChangeTask(t.getId()));
		return res;
	}

	/**
	 *  修改变更项目
	 */
	@RequestMapping(value = "/updateChangeTask", method = RequestMethod.POST)
	public Result updateChangeTask(InputTChangeTask t){
		Result res = new Result();
		if (null==t.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更项目id"));
			return res;
		}
		if (t.getType()!=3&&orderChangeService.checkTaskMain(t)){
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "", "主项目"));
			return res;
		}
		res.setData(orderChangeService.updateChangeTask(t));
		return res;
	}

	/**
	  *     修改变更催款
	 */
	@RequestMapping(value = "/updateChangeDun", method = RequestMethod.POST)
	public Result updateChangeDun(TChangeDun t){
		Result res = new Result();
		if (null==t.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更任务id"));
			return res;
		}
		res.setData(orderChangeService.updateChangeDun(t));
		return res;
	}

	/**
	 *  删除变更催款
	 */
	@RequestMapping(value = "/deleteChangeDun", method = RequestMethod.POST)
	public Result deleteChangeDun(TChangeDun t){
		Result res = new Result();
		if (null==t.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更任务id"));
			return res;
		}
		res.setData(orderChangeService.deleteChangeDun(t.getId()));
		return res;
	}

	/**
	 *	新增变更催款
	 */
	@RequestMapping(value = "/addChangeDun", method = RequestMethod.POST)
	public Result addChangeDun(TChangeDun d){
		Result res = new Result();
		if(StringUtils.isBlank(d.getOrderNo())||null==d.getCtid()) {
			res.getError().add(buildError("", "订单编号与项目编号必须指定"));
			return res;
		}
		if(null==d.getProjectType()||null==d.getDunType()) {
			res.getError().add(buildError("", "项目分类和催款分类必须指定"));
			return res;
		}
		res.setData(orderChangeService.addChangeDun(d));
		return res;
	}

	/**
	 *    订单查看变更列表
	 */
	@RequestMapping(value = "/orderChangeDetails", method = RequestMethod.GET)
	public Result orderChangeDetails(String orderNo){
		Result res = new Result();
		if(null==orderNo){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单编号"));
			return res;
		}
		res.setData(orderChangeService.orderChangeDetails( orderNo));
		return res;
	}

	/**
	 *    订单查看变更原订单
	 */
	@RequestMapping(value = "/orderChangeUsed", method = RequestMethod.GET)
	public Result orderChangeUsed(String orderNo){
		Result res = new Result();
		if(null==orderNo){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单编号"));
			return res;
		}
		res.setData(orderChangeService.orderChangeUsed( orderNo));
		return res;
	}

	/**
	 *    id查看变更详情
	 */
	@RequestMapping(value = "/orderChangeDetailsById", method = RequestMethod.GET)
	public Result orderChangeDetailsById(Integer id){
		Result res = new Result();
		if(null==id){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单变更编号"));
			return res;
		}
		res.setData(orderChangeService.orderChangeDetailsById( id));
		return res;
	}

	/**
	 * 变更修改、修改变更
	 * @param changeType 0发起修改 1财务修改
	 */
	@RequestMapping(value = "/updateOrderChange", method = RequestMethod.POST)
	public Result orderChangeDetails(NewOrderChangeBo t ,Integer changeType,String startRemarks){
		Result res = new Result();
		if(null==t.getId()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "订单变更id"));
			return res;
		}
		if (changeType==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "修改变更类型"));
			return res;
		}
		res.setData(orderChangeService.updateOrderChange(t,changeType,startRemarks));
		return res;
	}

	/**
	 *   变更审核
	 *    赠送：营销-经理-总经理审核-财务（已收款情况)-运营保障-总裁
	 *    重报：营销-经理-总经理审核-技术员-财务-财务总监-运营保障-总裁
	 *    仅项目：营销-经理-总经理审核-技术员-技术总监-财务-财务总监-运营保障-总裁
	 *    项目&金额：营销-经理-总经理审核-技术员-技术经理-技术总监-财务-财务总监-运营保障-总裁
	 *    退单退款：营销-经理-总经理审核-技术员-技术经理-技术总监-财务-财务总监-运营保障-总裁-董事长（大于2K，需董事长审核）-财务退票
	 *    退单不退款：营销-经理-总经理审核-技术员-技术经理-技术总监-财务-财务总监-运营保障-总裁-财务退票
	 *    仅金额：营销-经理-总经理审核-技术总监-财务-财务总监-运营保障-总裁
	 *    框架协议审核流程： 营销员发起-营销经理审核-营销管理员审核-财务审核-运营保障-完成
	 *    仅合同图片修改：营销-营销总经理-运营保障-完成
	 */
	@RequestMapping(value = "/orderChangeAudit", method = RequestMethod.POST)
	public Result orderChangeAudit(String changeId,String remarks,Integer status,Integer processState,Integer rejectState){
		Result res = new Result();
		if(Objects.isNull(changeId)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "订单编号", "订单编号"));
			return res;
		}
		if(Objects.isNull(status)){//2通过 3驳回
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "审核结果", "审核结果"));
			return res;
		}
		if(Objects.isNull(remarks)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "审核批示", "审核批示"));
			return res;
		}
		res.setData(orderChangeService.pushOrderChangeAudit( changeId, remarks, status, processState,rejectState));
		return res;
	}

	/**
	 *  变更日志
	 */
	@RequestMapping(value ="/orderChangeLogList",method = RequestMethod.GET)
	public Result orderChangeLogList(String changeId) {
		Result res =new Result();
		if (null==changeId) {
			res.getError().add(buildError("变更id错误", "变更id错误"));
			return res;
		}
		res.data(orderChangeService.selectOrderChangeLogList(changeId));
		return res;
	}

	/**
	 *	  变更文件上传
	 */
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public Result uploadRefundOrderFile(HttpServletRequest req,String sign){
		Result res = new Result();
		//order_refund_file
		res.setData(handleFile(res, "/order_change_file/", false, req, sign));
		return res;
	}

	/**
	 * 	超级无语接口，变更太乱了
	 * 	首先是草稿然后发起变成审核中，再在流程中走一遍当金额小于2000总裁通过标记为通过，大于2000总裁通过不改变状态，流转到董事长，董事长审核改变
	 * 	状态为通过，通过状态下为财务退票，退票完成后改状态为上传附件，实际为营销员上传附件，营销员上传完后触发完成变更，状态才变成完成
	 * 	状态 0草稿 1审核中 2通过 3驳回 4完成 5撤销 6上传附件
	 * @param processState 流程状态 0=营销员，1=营销经理，2=营销管理员，3=技术员，4=技术经理，5=技术总监，6=财务专员，7=财务总监，8运营保障 9=总裁，10=董事长
	 */
	@RequestMapping(value ="/orderChangeList",method = RequestMethod.GET)
	public Result orderChangeList(String userName, Integer  processState, Integer timeType, String startTime, String endTime,
								  String deps, String salesmanName, Integer complete, String orderNo, String contractNo, Integer type, Integer pageSize, Integer pageNo) {
		Result res =new Result();
		res.data(orderChangeService.selectOrderChangeList( userName,  processState, timeType, startTime, endTime,
				 deps, salesmanName, complete, orderNo,  contractNo, type,pageSize,  pageNo));
		return res;
	}

	/**
	 *  新增回收发票
	 */
	@RequestMapping(value ="/addOrderRefundInvoice",method = RequestMethod.POST)
	public Result addOrderRefundInvoice(OrderRefundInvoice o) {
		Result res =new Result();
		if (o.getAmount()==null) {
			res.getError().add(buildError("金额不能为空", "金额不能为空"));
			return res;
		}
		res.data(orderChangeService.addOrderRefundInvoice(o));
		return res;
	}

	/**
	 *  新增回收发票
	 */
	@RequestMapping(value ="/getOrderChange",method = RequestMethod.GET)
	public Result getOrderChange(String  orderNo) {
		Result res =new Result();
		if (StringUtils.isBlank(orderNo)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "订单编号", "订单编号"));
			return res;
		}
		res.data(orderChangeService.getOrderChange(orderNo));
		return res;
	}

	/**
	 *  删除回收发票
	 */
	@RequestMapping(value ="/deleteOrderRefundInvoice",method = RequestMethod.POST)
	public Result deleteOrderRefundInvoice(Integer id) {
		Result res =new Result();
		if (id==null) {
			res.getError().add(buildError("id不能为空", "id不能为空"));
			return res;
		}
		res.data(orderChangeService.deleteOrderRefundInvoice(id));
		return res;
	}

	/**
	 *  回收发票列表
	 */
	@RequestMapping(value ="/listOrderRefundInvoice",method = RequestMethod.GET)
	public Result listOrderRefundInvoice(String orderNo) {
		Result res =new Result();
		if (StringUtils.isBlank(orderNo)) {
			res.getError().add(buildError("订单编号不能为空", "订单编号不能为空"));
			return res;
		}
		res.data(orderChangeService.listOrderRefundInvoice(orderNo));
		return res;
	}

	/**
	 *  回收发票列表
	 */
	@RequestMapping(value ="/listOrderInvoiceAndBill",method = RequestMethod.GET)
	public Result listOrderInvoiceAndBill(String orderNo) {
		Result res =new Result();
		if (StringUtils.isBlank(orderNo)) {
			res.getError().add(buildError("订单编号不能为空", "订单编号不能为空"));
			return res;
		}
		res.data(orderChangeService.listOrderInvoiceAndBill(orderNo));
		return res;
	}

	/**
	 * 完成变更、变更完成
	 */
	@RequestMapping(value ="/completeOrderChange",method = RequestMethod.GET)
	public Result completeOrderChange(NewOrderChangeBo nb) {
		Result res =new Result();
		if (StringUtils.isBlank(nb.getOrderNo())||nb.getType()==null) {
			res.getError().add(buildError("订单编号和类型不能为空", "订单编号和类型不能为空"));
			return res;
		}
		if (orderChangeService.checkChangeId(nb.getId())){
			StringBuilder str=new StringBuilder("操作异常");
            str.append("[").append(nb.getId()).append("]").append("编号变更已经完成");
            res.getError().add(buildError(str.toString(), str.toString()));
			return res;
		}
		res.data(orderChangeService.pushCompleteOrderChange(nb));
		return res;
	}

	/**
	 * 完成变更退票
	 */
	@RequestMapping(value ="/completeRefund",method = RequestMethod.GET)
	public Result completeRefund(NewOrderChangeBo nb) {
		Result res =new Result();
		if (nb.getId()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,ErrorConstants.PARAM_EMPTY_ERROR,"变更编号"));
			return res;
		}
		res.data(orderChangeService.pushCompleteRefund(nb));
		return res;
	}

	/**
	 * 取消变更
	 */
	@RequestMapping(value ="/cancelOrderChange",method = RequestMethod.POST)
	public Result cancelOrderChange(NewOrderChangeBo nb) {
		Result res =new Result();
		if (null==nb.getId()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "变更编号", "变更编号"));
			return res;
		}
		if (nb.getProcessState()!=0&&(nb.getStatus()!=0 && nb.getStatus()!=3)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "变更流程或状态", "变更流程或状态"));
			return res;
		}
		res.data(orderChangeService.updateCancelOrderChange(nb));
		return res;
	}

	/**
	 * 	导出变更列表
     *
     */
    @RequestMapping(value = "/exportMyChange" , method = RequestMethod.GET)
    public Result exportMyBill(HttpServletResponse response,String userName,Integer  processState,Integer timeType,String startTime,String endTime,
							   String deps,String salesmanName,Integer complete,String orderNo, String contractNo,Integer type,Integer pageSize, Integer pageNo)  {
    	Result res=new Result();
			try {
				orderChangeService.exportMyChange(response,userName,processState,timeType,startTime,endTime,deps,salesmanName,complete,
				orderNo,contractNo,type,pageSize,pageNo);
			} catch (IOException e) {
				res.getError().add(buildError("格式不正确"));
				e.printStackTrace();
				return res;
			}
         res.data(1);
    	return res;
    }

    /**
	 *	  变更退款图片文件上传
	 */
	@RequestMapping(value = "/uploadRefund", method = RequestMethod.POST)
	public Result uploadRefund(HttpServletRequest req,String sign){
		Result res = new Result();
		//order_refund_file
		res.setData(handleFile(res, "/order_refund/", false, req, sign));
		return res;
	}

	/**
	 *	  变更附件文件上传
	 */
	@RequestMapping(value = "/uploadChangeAttachment", method = RequestMethod.POST)
	public Result uploadChangeAttachment(HttpServletRequest req,String sign){
		Result res = new Result();
		res.setData(handleFile(res, "/order_change_attachment/", false, req, sign));
		return res;
	}

	/**
	 *	  新增退款信息
	 */
	@RequestMapping(value = "/addRefund", method = RequestMethod.POST)
	public Result addRefund(InputNewOrderRefund or){
		Result res = new Result();
		if (or.getRefundAmount()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"金额", "金额"));
			return res;
		}
		if (or.getRefundDate()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"时间", "时间"));
			return res;
		}
		if (orderChangeService.checkrefund(or)) {
			res.getError().add(buildError("金额与实际收款不符", "金额与实际收款不符"));
			return res;
		}
		res.setData(orderChangeService.addOrderRefund(or));
		return res;
	}

	/**
	 * 删除退款信息
	 */
	@RequestMapping(value = "/deleteRefund", method = RequestMethod.POST)
	public Result deleteRefund(Integer id){
		Result res = new Result();
		res.setData(orderChangeService.deleteRefund(id));
		return res;
	}

	/**
	 * 退款信息列表
	 */
	@RequestMapping(value = "/listRefund", method = RequestMethod.GET)
	public Result listRefund(Integer id){
		Result res = new Result();
		res.setData(orderChangeService.listRefund(id));
		return res;
	}

	/**
	 * 确认退款
	 */
	@RequestMapping(value = "/pushRefund", method = RequestMethod.POST)
	public Result pushRefund(Integer id,String refundUrl){
		Result res = new Result();
		res.setData(orderChangeService.pushRefund(id,refundUrl));
		return res;
	}

	/**
	 * 变更-新增会员子项目
	 *
	 */
	@RequestMapping(value = "" +
			"/addMemberSonProject" ,method = RequestMethod.POST)
	public Result addMemberSonProject(InputAddSonProject in ){
		Result res = new Result();
		if(in.getId()==null||StringUtils.isBlank(in.getServiceYear())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","参数"));
			return res;
		}
		if(orderChangeService.checkMember(in)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","高新会员"));
			return res;
		}
		Object o = orderChangeService.addMemberSonProject(in);
		orderChangeService.pushSuerProject( in);
		return res.data(o);

	}

	/**
	 * 删除会员子项目
	 *
	 */
	@RequestMapping(value = "/deleteMemberSonProject" ,method = RequestMethod.POST)
	public Result deleteMemberSonProject(Integer id){
		Result res = new Result();
		if(id==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		return res.data(orderChangeService.deleteMemberSonProject( id));
	}



	/**
	 * 新增会员子项目
	 *
	 */
	@RequestMapping(value = "/updateMemberSonProject" ,method = RequestMethod.POST)
	public Result updateMemberSonProject(InputAddSonProject in ){
		Result res = new Result();
		if(in.getId()==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","参数"));
			return res;
		}
		if(orderChangeService.checkMember(in)){
//		if(in.getHtMember()==0&&in.getAdditionalDeduction()==0&&in.getRdAwardsubsidy()==0){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","高新会员"));
			return res;
		}
		return res.data(orderChangeService.updateMemberSonProject( in));
	}


	/**
	 * 获取变更流程名称
	 */
	@RequestMapping(value = "/getChangeProcessName" ,method = RequestMethod.GET)
	public Result getChangeProcessName(Integer id){
		Result res = new Result();
		if(id==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","变更编号"));
			return res;
		}
		return res.data(orderChangeService.gerProcessName(id));
	}
}
