package com.goafanti.weChat.controller;


import com.goafanti.common.bo.Error;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.error.BusinessException;
import com.goafanti.common.model.OrderPublicReleaseLog;
import com.goafanti.common.model.PublicConfig;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.weChat.bo.*;
import com.goafanti.weChat.service.PublicReleaseService;
import org.apache.commons.beanutils.BeanUtils;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

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

	@Resource
	private PublicReleaseService publicReleaseService;

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



	/**
	 * 发起外出申请、发起公出
	 */
	@RequestMapping(value = "/addPublicRelease", method = RequestMethod.POST)
	public Result addPublicRelease(InputPublicRelease in){
		Result res = new Result();
		if (StringUtils.isBlank(in.getUids())) {
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"用户编号"));
			return res;
		}
		if (StringUtils.isBlank(in.getReleaseStarts())) {
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"公出时间"));
			return res;
		}
		if (StringUtils.isBlank(in.getDistrictName())) {
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"公出地点"));
			return res;
		}
		int i = publicReleaseService.checkTime(in);
		if (i==1) {
			res.getError().add(buildError("发起人公出时段已经被使用！","发起人公出时段已经被使用！"));
			return res;
		}
		if (i==2) {
			res.getError().add(buildError("协单人公出时段已经被使用！","协单人公出时段已经被使用！"));
			return res;
		}
		if(in.getAssist()==null)in.setAssist(0);
		if (in.getAssist()==1&&in.getAssistAid()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"协单人员"));
			return res;
		}
		if (publicReleaseService.checkPublicReviewer(in)){
			res.getError().add(buildError("请让管理员先设置公出审核人。"));
			return res;
		}
		res.setData(publicReleaseService.addPublicRelease(in));
		return res;
	}

	/**
	 * 判定客户是否完成了客户档案
	 */
	@RequestMapping(value = "/checkUserArchives", method = RequestMethod.GET)
	public Result checkUserArchives(String uid){
		Result res = new Result();
		if (StringUtils.isBlank(uid)) {
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"用户编号"));
			return res;
		}
		//小程序去掉判定，先改接口
//		res.setData(publicReleaseService.checkUserArchives(uid));
		res.setData(true);
		return res;
	}

	/**
	 * 修改外出申请、修改公出
	 */
	@RequestMapping(value = "/updatePublicRelease", method = RequestMethod.POST)
	public Result updatePublicRelease(InputPublicRelease in){
		Result res = new Result();
		if (in.getId()==null){
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"编号"));
			return res;
		}
		if (in.getStatus()!=3) {
			int i = publicReleaseService.checkTime(in);
			if (i==1) {
				res.getError().add(buildError("发起人公出时段已经被使用！","发起人公出时段已经被使用！"));
				return res;
			}
			if (i==2) {
				res.getError().add(buildError("协单人公出时段已经被使用！","协单人公出时段已经被使用！"));
				return res;
			}
		}
		res.setData(publicReleaseService.updatePublicRelease(in));
		return res;
	}

	/**
	 * 修改打卡地址
	 */
	@RequestMapping(value = "/updateLocation", method = RequestMethod.POST)
	public Result updateLocation(InputPublicRelease in){
		Result res = new Result();
		if (in.getId()==null||in.getUid()==null){
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"编号"));
			return res;
		}
		if (in.getLatitude()==null||in.getLongitude()==null||in.getDistrictName()==null){
			res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"位置"));
			return res;
		}
		res.setData(publicReleaseService.updateLocation(in));
		return res;
	}

	/**
	 * 外出打卡、公出打卡
	 */
	@RequestMapping(value = "/publicReleaseClockIn", method = RequestMethod.POST)
	public Result publicReleaseClockIn(Integer id,String photoUrl,Integer clockIn ,String clockInRemarks,String uid ){
		Result res =new Result();
		int x=publicReleaseService.pushPublicReleaseClockIn(id,photoUrl,clockIn,clockInRemarks,uid);
		if (x==-1){
			res.getError().add(buildError("公出他人企业！需通过审核，才可以打卡！"));
			return res;
		}
//		取消打卡限制
		if (x==-2){
			res.getError().add(buildError("公出审核中，不可打卡！"));
			return res;
		}
		if (x==-3){
			res.getError().add(buildError("技术协单，必须先写协单意见，才可打卡！"));
			return res;
		}
		res.setData(x);
		return res;
	}

	/**
	 * 外出申请详情、公出详情
	 */
	@RequestMapping(value = "/dtails", method = RequestMethod.GET)
	public Result details(Integer id){
		Result res =new Result();
		res.setData(publicReleaseService.dtails(id));
		return res;
	}


	/**
	 * 外出申请详情
	 */
	@RequestMapping(value = "/followDtails", method = RequestMethod.GET)
	public Result followDetails(String id){
		Result res =new Result();
		res.setData(publicReleaseService.followDtails(id));
		return res;
	}

	/**
	 * 外出申请列表.公出列表,我的协单列表,协单审核，
	 */
	@RequestMapping(value = "/listPublicRelease", method = RequestMethod.GET)
	public Result listPublicRelease(InputPublicReleaseList in){
		Result res =new Result();
		res.setData(publicReleaseService.listPublicRelease(in));
		return res;
	}

	/**
	 * 公出审核、外出审核 上级
	 */
	@RequestMapping(value = "/examinePublicRelease", method = RequestMethod.POST)
	public Result examinePublicRelease(Integer id ,Integer status,String remarks,Integer evaluateType){
		Result res =new Result();
		if (id==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"公出编号"));
			return res;
		}
		if (status!=0&&status!=2) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"审核状态"));
			return res;
		}
//		if (remarks.length()>20) {
//			res.getError().add(buildError("备注长度不能超过20个字符。"));
//			return res;
//		}
		res.setData(publicReleaseService.pushExaminePublicRelease(id,status,remarks,0,evaluateType));
		return res;
	}

	/**
	 * 公出审核、外出审核 客户归属人
	 */
	@RequestMapping(value = "/marketersExamine", method = RequestMethod.POST)
	public Result marketersExamine(Integer id ,Integer status,String remarks,Integer evaluateType){
		Result res =new Result();
		if (id==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"公出编号"));
			return res;
		}
		if (status!=0&&status!=2) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"审核状态"));
			return res;
		}
		if (remarks.length()>20) {
			res.getError().add(buildError("备注长度不能超过20个字符。"));
			return res;
		}
		res.setData(publicReleaseService.pushExaminePublicRelease(id,status,remarks,1, evaluateType));
		return res;
	}


	/**
	 * 公出审核、外出审核 协单审核
	 */
	@RequestMapping(value = "/assistExamine", method = RequestMethod.POST)
	public Result assistExamine(Integer id ,Integer status,String remarks,Integer evaluateType){
		Result res =new Result();
		if (id==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"公出编号"));
			return res;
		}
		if (status!=0&&status!=2) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"审核状态"));
			return res;
		}
		if (remarks.length()>20) {
			res.getError().add(buildError("备注长度不能超过20个字符。"));
			return res;
		}
		res.setData(publicReleaseService.updateAssistExamine(id,status,remarks,evaluateType));
		return res;
	}


	/**
	 * 技术公出审核 公出他人企业
	 */
	@RequestMapping(value = "/techExamine", method = RequestMethod.POST)
	public Result techExamine(Integer id ,Integer status,String remarks,Integer evaluateType){
		Result res =new Result();
		if (id==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"公出编号"));
			return res;
		}
		if (status!=0&&status!=2) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"审核状态"));
			return res;
		}
		if (remarks.length()>20) {
			res.getError().add(buildError("备注长度不能超过20个字符。"));
			return res;
		}
		res.setData(publicReleaseService.pushExaminePublicRelease(id,status,remarks,2,evaluateType));
		return res;
	}


	/**
	 * 咨询驳回
	 */
	@RequestMapping(value = "/techReject", method = RequestMethod.POST)
	public Result techReject(Integer id ,String remarks){
		Result res =new Result();
		if (id==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"公出编号"));
			return res;
		}
		if (remarks.length()>20) {
			res.getError().add(buildError("备注长度不能超过20个字符。"));
			return res;
		}
		res.setData(publicReleaseService.pushTechReject( id , remarks));
		return res;
	}



	/**
	 * 外出审核日志列表
	 */
	@RequestMapping(value = "/listPublicReleaseLog", method = RequestMethod.GET)
	public Result listPublicReleaseLog(Integer id,String ufid){
		Result res =new Result();
		res.data(publicReleaseService.listPublicReleaseLog(id,ufid));
		return res;
	}



	/**
	 * 公出统计
	 */
	@RequestMapping(value = "/publicReleaseStatistics", method = RequestMethod.GET)
	public Result publicReleaseStatistics(InputPublicStatistics in){
		Result res =new Result();
		res.data(publicReleaseService.publicReleaseStatistics(in));
		return res;
	}

	/**
	 * 公出统计导出
	 */
	@RequestMapping(value = "/publicReleaseStatistics/export", method = RequestMethod.GET)
	public Result publicReleaseStatisticsExport(InputPublicStatistics in,HttpServletResponse response){
		List<OutPublicStatistics> list =publicReleaseService.publicReleaseStatisticsList(in);
		NewExcelUtil<OutPublicStatistics>excel=new NewExcelUtil<>(OutPublicStatistics.class);
  		 return  excel.exportExcel(list,"公出统计列表",response);
	}


	/**
	 * 公出详情列表
	 */
	@RequestMapping(value = "/publicReleaseDtails", method = RequestMethod.GET)
	public Result publicReleaseListDtails(InputPublicDtails in){
		Result res =new Result();
		res.data(publicReleaseService.publicReleaseListDtails(in));
		return res;
	}


	@RequestMapping(value = "/publicReleaseAndCount", method = RequestMethod.GET)
	public Result publicReleaseAndCount(InputPublicDtails in){
		Result res =new Result();
		res.data(publicReleaseService.publicReleaseAndCount(in));
		return res;
	}

	/**
	 * 公出详情列表
	 */
	@RequestMapping(value = "/publicReleaseDtails/export", method = RequestMethod.GET)
	public Result publicReleaseListDtailsExport(InputPublicDtails in ,Integer exportType){
		if (exportType==null)exportType=0;
		List<OutPublicDtails> outList=publicReleaseService.publicReleaseListDtailsList(in);
		publicReleaseService.pushOutPublicDtails(outList);
		if (exportType==0){
            NewExcelUtil<OutPublicDtails>excel=new NewExcelUtil<>(OutPublicDtails.class);
			return  excel.exportExcel(outList,"公出详细列表",uploadPath);
		}else if (exportType==1){
			List<OutPublicDtailsFinance> list = new ArrayList<>();
			for (OutPublicDtails outPublicDtails : outList) {
				OutPublicDtailsFinance of= new OutPublicDtailsFinance();
				try {
					BeanUtils.copyProperties( of,outPublicDtails);
				} catch (Exception e) {
					throw new BusinessException("数据转换异常");
				}
				list.add(of);
			}
			NewExcelUtil<OutPublicDtailsFinance>excel=new NewExcelUtil<>(OutPublicDtailsFinance.class);
			return  excel.exportExcel(list,"公出详细列表",uploadPath);
		}
		return null;
	}

	/** 上传图片 **/
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public Result uploadOrderInvoiceFile(HttpServletRequest req){
		Result res = new Result();
		res.setData(handleFile(res, "/publicRelease/", false, req, "publicRelease"));
		return res;
	}

	/**
	 * 搜索
	 */
	@RequestMapping(value = "/addSupplement", method = RequestMethod.POST)
	public Result addSupplement(Integer id,String supplement,String nextPlan){
		Result res =new Result();
		if (id==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"公出编号"));
			return res;
		}
		res.setData(publicReleaseService.addSupplement( id ,supplement,nextPlan));
		return res;
	}

	/**
	 * 根据客户返回订单
	 */
	@RequestMapping(value = "/selectOrderByUid",method =RequestMethod.GET)
	public Result selectOrderByUid(String uid){
		Result res = new Result();
		if (uid==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,ErrorConstants.PARAM_EMPTY_ERROR,"客户编号"));
			return res;
		}
		res.data(publicReleaseService.selectOrderByUid(uid));
		return res;
	}

	/**
	 * 查看上门记录
	 */
	@RequestMapping(value = "/publicByOrder",method =RequestMethod.GET)
	public Result publicByOrder(String orderNo){
		Result res = new Result();
		if (orderNo==null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,ErrorConstants.PARAM_EMPTY_ERROR,"订单编号"));
			return res;
		}
		res.data(publicReleaseService.publicByOrder(orderNo));
		return res;
	}

	/**
	 * 打卡页面获取自己最新的打卡
	 */
	@RequestMapping(value = "/getMyNewPublic",method = RequestMethod.GET)
	public Result getMyNewPublic (){
		Result res =new Result();
		res.data(publicReleaseService.getMyNewPublic());
		return res;
	}

	/**
	 * 新增协单助手
	 * @param id 公出编号
	 * @param aid 协单助手编号
	 */
	@RequestMapping(value = "/addAssistant",method = RequestMethod.POST)
	public Result addAssistant (Integer id,String aid){
		Result res =new Result();
		if (id==null||aid==null){
			res.getError().add(new Error(ErrorConstants.PARAM_EMPTY_ERROR,"公出编号与人员编号"));
			return res;
		}
		if (publicReleaseService.checkaddAssistant(id,aid)){
			res.getError().add(new Error("协单助手已经存在"));
			return res;
		}
		res.data(publicReleaseService.addAssistant(id,aid));
		return res;
	}

	/**
	 * 删除协单助手
	 * @param id 公出编号
	 * @param aid 协单助手编号
	 */
	@RequestMapping(value = "/deleteAssistant",method = RequestMethod.POST)
	public Result deleteAssistant (Integer id,String aid){
		Result res =new Result();
		if (id==null||aid==null){
			res.getError().add(new Error(ErrorConstants.PARAM_EMPTY_ERROR,"公出编号与人员编号"));
			return res;
		}
		if (publicReleaseService.checkdeleteAssistant(id,aid)){
			res.getError().add(new Error("协单助手已经打卡，无法删除"));
			return res;
		}
		res.data(publicReleaseService.deleteAssistant(id,aid));
		return res;
	}


	/**
	 * 技术发起公出查询发起情况
	 */
	@RequestMapping(value = "/checkOrderNoDuration",method = RequestMethod.GET)
	public Result checkOrderNoDuration (InputPublicRelease in){
		Result res =new Result();
		return res.data(publicReleaseService.checkOrderNoDuration(in));
	}


	/**
	 * 技术发起公出查询发起情况
	 */
	@RequestMapping(value = "/pushDateClock",method = RequestMethod.POST)
	public Result pushDateClock (){
		Result res =new Result();
		return res.data(publicReleaseService.pushDateClock());
	}
	/**
	 * 公出&报销表
	 */
	@RequestMapping(value = "/releaseAndExpenseCount",method = RequestMethod.GET)
	public Result releaseAndExpenseCount (InputreleaseAndExpenseCount in){
		Result res =new Result();
		return res.data(publicReleaseService.releaseAndExpenseCount(in));
	}/**
	 * 公出&报销缓存清除
	 */
	@RequestMapping(value = "/releaseAndExpenseCountClear",method = RequestMethod.GET)
	public Result releaseAndExpenseCountClear (){
		Result res =new Result();
		return res.data(publicReleaseService.releaseAndExpenseCountClear());
	}

	/**
	 * 公出&报销表导出
	 */
	@RequestMapping(value = "/releaseAndExpenseCount/Export",method = RequestMethod.GET)
	public Result releaseAndExpenseCountExport (InputreleaseAndExpenseCount in){
		List<releaseAndExpenseCountOut> list = publicReleaseService.releaseAndExpenseCount(in);
		NewExcelUtil<releaseAndExpenseCountOut> excelUtil=new NewExcelUtil<>(releaseAndExpenseCountOut.class);
		return excelUtil.exportExcel(list,"公出与报销表",uploadPath);
	}


	/**
	 * 协单审核是否有未审核查询
	 */
	@RequestMapping(value = "/unaudited",method = RequestMethod.GET)
	public Result assistUnaudited (){
		Result res =new Result();
		return res.data(publicReleaseService.assistUnaudited());
	}


	/**
	 * 分享限时查询
	 */
	@RequestMapping(value = "/limitUser",method = RequestMethod.POST)
	public Result limitUser (String uid,Integer hours){
		Result res =new Result();
		int i = publicReleaseService.pushLimitUser(uid,hours);
		if (i==0){
			return res().error(buildError(ErrorConstants.PARAM_ERROR,"只有客户归属人可以分享"));
		}
		return res.data(i);
	}

	/**
	 * 修改最大公出
	 */
	@RequestMapping(value = "/updateMaxDuration",method = RequestMethod.POST)
	public Result updateMaxDuration (String orderNo, Double maxDuration, String remarks){
		Result res =new Result();
		if (StringUtils.isEmpty(orderNo)){
			return res().error(buildError(ErrorConstants.PARAM_ERROR,"订单编号"));
		}
		if (maxDuration.isNaN()){
			return res().error(buildError(ErrorConstants.PARAM_ERROR,"最大公出"));
		}

		return res.data(publicReleaseService.updateMaxDuration(orderNo,maxDuration,remarks));
	}

	/**
	 * 审核最大公出
	 */
	@RequestMapping(value = "/examineMaxDuration",method = RequestMethod.POST)
	public Result examineMaxDuration (String orderNo,Integer status,String remarks){
		Result res =new Result();
		if (StringUtils.isEmpty(orderNo)){
			return res().error(buildError(ErrorConstants.PARAM_ERROR,"订单编号"));
		}
		return res.data(publicReleaseService.pushExamineMaxDuration(orderNo,status,remarks));
	}


	/**
	 * 最大公出日志
	 */
	@RequestMapping(value = "/maxDurationLog",method = RequestMethod.GET)
	public Result<List<OrderPublicReleaseLog>> maxDurationLog (String orderNo){
		Result res =new Result();
		if (StringUtils.isEmpty(orderNo)){
			return res().error(buildError(ErrorConstants.PARAM_ERROR,"订单编号"));
		}
		return res.data(publicReleaseService.maxDurationLog(orderNo));
	}



	/**
	 * 修改最大公出审核配置
	 */
	@RequestMapping(value = "/updatePublicConfig",method = RequestMethod.POST)
	public Result updatePublicConfig (PublicConfig in){
		Result res =new Result();

		return res.data(publicReleaseService.updatePublicConfig(in));
	}

	/**
	 * 获取最大公出审核配置
	 */
	@RequestMapping(value = "/getPublicConfig",method = RequestMethod.GET)
	public Result<OutPublicConfig> getPublicConfig (){
		Result res =new Result();
		return res.data(publicReleaseService.getPublicConfig());
	}
}
