package com.xxfc.platform.tour.biz;

import cn.hutool.core.date.DateUtil;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import com.xxfc.platform.tour.vo.TourGoodOrderFindVo;
import com.xxfc.platform.tour.vo.TourGoodOrderVo;
import com.xxfc.platform.tour.vo.TourVerificationInfoVo;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Service;
import  com.xxfc.platform.tour.entity.TourGoodVerification;
import  com.xxfc.platform.tour.mapper.TourGoodVerificationMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 旅游线路核销明细
 *
 * @author zjw
 * @email nishijjo@qq.com
 * @date 2019-06-14 09:36:50
 */
@Service
public class TourGoodVerificationBiz extends BaseBiz<TourGoodVerificationMapper,TourGoodVerification> {

    //删除线路核销
    public  void delGoodVerification(Integer goodId,List<Integer> ids){ mapper.delVerification(goodId,ids);}

    /**
     * 批量插入核销
     * @param tourGoodVerifications
     */
    public  void addBathTourGoodVerification(List<TourGoodVerification> tourGoodVerifications){
        for (TourGoodVerification tourGoodVerification:tourGoodVerifications){
            mapper.insertTourGoodVerification(tourGoodVerification);
        }
    }

    /**
     * 分页查询订单列表
     * @param tourGoodOrderFindVo
     * @return
     */
    public PageDataVO<TourGoodOrderVo> findTourGoodOrdersPage(TourGoodOrderFindVo tourGoodOrderFindVo) {

         Date trvaelTime = tourGoodOrderFindVo.getTravelTime();
        String dateStr = DateUtil.format(trvaelTime, "YYYY-MM-dd");
        tourGoodOrderFindVo.setTravelDate(dateStr);
        PageDataVO<TourGoodOrderVo>  tourGoodOrderVoPageDataVO= PageDataVO.pageInfo(tourGoodOrderFindVo.getPage(), tourGoodOrderFindVo.getLimit(), () -> mapper.findVerificationAll(tourGoodOrderFindVo));
        List<TourGoodOrderVo> tourGoodOrderVos = tourGoodOrderVoPageDataVO.getData();
         tourGoodOrderVos.stream().peek(tourGoodOrderVo -> {
            tourGoodOrderVo.setLeaveOfNum(tourGoodOrderVo.getHeadCount()-tourGoodOrderVo.getTripOfNum());
            String startDateStr = DateUtil.format(tourGoodOrderVo.getStartDate(),"YYYY-MM-dd");
             tourGoodOrderVo.setTravelTime(startDateStr);

             Long startTime = tourGoodOrderVo.getStartTime();
             if (null!=startTime){
               /*  LocalTime localTime = LocalTime.ofSecondOfDay(startTime);
                 String timeStr = String.format("%s:%s:%s",localTime.getHour()==0?"00":localTime.getHour(),
                         localTime.getMinute()==0?"00":localTime.getMinute(),localTime.getSecond()==0?"00":localTime.getSecond());*/
                 String timeStr=new SimpleDateFormat("HH:mm").format(startTime);
                 tourGoodOrderVo.setTravelTime(String.format("%s %s",tourGoodOrderVo.getTravelTime(),timeStr));
             }
        }).count();
        return tourGoodOrderVoPageDataVO;
    }

    /**
     * 更改发车状态
      * @param verficationId
     * @return
     */
    public int updateTourGoodVerificationStatus(Integer verficationId){
        return mapper.updateStatusByGoodIdAndSpeIdAndSiteId(verficationId,1);
    }

    /**
     * 更新总人数 或 上车人数
      * @param verficationId
     * @param properties
     * @return
     */
    public int updateTourGoodPersonNum(Integer verficationId,String properties,Integer number){

         return mapper.updateTourGoodPerNumbs(verficationId,properties,number);
    }

    public Integer selectDepartureStatusByVerificationId(Integer verificationId) {
        TourGoodVerification tourGoodVerification = new TourGoodVerification();
        tourGoodVerification.setId(verificationId);
        TourGoodVerification verfication = mapper.selectByPrimaryKey(tourGoodVerification);
        return verfication.getStatus();
    }

    //获取路线核销id
    public ObjectRestResponse getVerificationList(TourVerificationInfoVo tourVerificationInfoVo){
        Integer companyId=tourVerificationInfoVo.getCompanyId();
        Integer goodId=tourVerificationInfoVo.getGoodId();
        Integer siteId=tourVerificationInfoVo.getSiteId();
        List<Integer> companyIds=tourVerificationInfoVo.getCompanyList();
        List<TourVerificationInfoVo> list=null;
        if (goodId==null||goodId==0){
            if (companyId!=null&&companyId!=0){
                list=mapper.getSiteList(null,companyId,null);
                if (list.size()==0){
                    return  ObjectRestResponse.succ();
                }
            }else if (companyIds!=null&&companyIds.size()>0){
                list=mapper.getSiteList(null,null,companyIds);
            }
            list=mapper.getGoodList(list);
        }else {
            if (siteId==null||siteId==0){
                if (companyId==null||companyId==0){
                    list=mapper.getSiteList(goodId,null,companyIds);
                }else {
                    list=mapper.getSpeList(goodId,null,companyId);
                }
            }else {
                list= mapper.getSpeList(goodId,siteId,null);
            }

        }
        return  ObjectRestResponse.succ(list);
    }
    //获取路线核销信息
    public ObjectRestResponse getVerification(Integer verificationId){
        return  ObjectRestResponse.succ(mapper.getVerification(verificationId));
    }


}