package com.goafanti.weChat.controller;


import java.util.ArrayList;
import java.util.List;

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

import com.goafanti.common.bo.Error;
import com.goafanti.common.error.BusinessException;
import com.goafanti.weChat.bo.*;
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 com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.weChat.service.PublicReleaseService;

@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;
		}
		if (publicReleaseService.checkTime(in)) {
			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;
		}
		res.setData(publicReleaseService.addPublicRelease(in));
		return res;
	}

	/**
	 * 修改外出申请
	 * @return
	 */
	@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&&publicReleaseService.checkTime(in)) {
			res.getError().add(buildError("公出时段已经被使用！","公出时段已经被使用！"));
			return res;
		}
		res.setData(publicReleaseService.updatePublicRelease(in));
		return res;
	}

	/**
	 * 修改打卡地址
	 * @param in
	 * @return
	 */
	@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;
	}

	/**
	 * 外出打卡、公出打卡
	 * @return
	 */
	@RequestMapping(value = "/publicReleaseClockIn", method = RequestMethod.POST)
	public Result publicReleaseClockIn(Integer id,String photoUrl,Integer clockIn ,String clockInRemarks,String uid ){
		Result res =new Result();

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


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

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

	/**
	 * 公出审核、外出审核
	 * @return
	 */
	@RequestMapping(value = "/examinePublicRelease", method = RequestMethod.POST)
	public Result examinePublicRelease(Integer id ,Integer status,String remarks){
		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));
		return res;
	}


	/**
	 * 咨询驳回
	 * @return
	 */
	@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;
	}



	/**
	 * 外出审核日志列表
	 * @return
	 */
	@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;
	}



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

	/**
	 * 公出统计导出
	 * @return
	 */
	@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);
	}


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

	/**
	 * 公出详情列表
	 * @return
	 */
	@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);
		if (exportType==0){
			List<OutPublicDtails> list=outList;
			NewExcelUtil<OutPublicDtails>excel=new NewExcelUtil<>(OutPublicDtails.class);
			return  excel.exportExcel(list,"公出详细列表",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;
	}

	/**
	 * 搜索
	 * @return
	 */
	@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;
		}
		if (supplement==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"本次公出目标总结"));
			return res;
		}
		if (nextPlan==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;
	}

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

	/**
	 * 新增协单助手
	 * @param id 公出编号
	 * @param aid 协单助手编号
	 * @return
	 */
	@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 协单助手编号
	 * @return
	 */
	@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;
	}



}
