package com.sqx.modules.artificer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserMoney;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.artificer.dao.ArtificerDao;
import com.sqx.modules.artificer.dao.ArtificerTimeDao;
import com.sqx.modules.artificer.dao.OrdersDao;
import com.sqx.modules.artificer.entity.Artificer;
import com.sqx.modules.artificer.entity.ArtificerTime;
import com.sqx.modules.artificer.entity.CollectArtificer;
import com.sqx.modules.artificer.entity.Orders;
import com.sqx.modules.artificer.service.ArtificerService;
import com.sqx.modules.artificer.service.ArtificerTimeService;
import com.sqx.modules.artificer.service.CollectArtificerService;
import com.sqx.modules.pay.dao.CashOutDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;


@Service
public class ArtificerTimeServiceImpl extends ServiceImpl<ArtificerTimeDao, ArtificerTime> implements ArtificerTimeService {

    @Autowired
    private ArtificerService artificerService;
    @Autowired
    private OrdersDao ordersDao;

    @Override
    public Result updateArtificerTime(String date,String times,Long userId){
        Artificer artificer = artificerService.selectArtificerByUserId(userId);
        baseMapper.delete(new QueryWrapper<ArtificerTime>().eq("artificer_date",date).eq("classify",2).eq("artificer_id",artificer.getArtificerId()));
        if(StringUtils.isNotEmpty(times)){
            for(String time:times.split(",")){
                Integer counts = baseMapper.selectCount(new QueryWrapper<ArtificerTime>().eq("artificer_date", date).eq("artificer_id", artificer.getArtificerId()).eq("artificer_time", time));
                if(counts==0){
                    ArtificerTime artificerTime=new ArtificerTime();
                    artificerTime.setArtificerDate(date);
                    artificerTime.setArtificerTime(time);
                    artificerTime.setArtificerId(artificer.getArtificerId());
                    artificerTime.setClassify(2);
                    artificerTime.setCreateTime(DateUtils.format(new Date()));
                    baseMapper.insert(artificerTime);
                }
            }
        }

        return Result.success();
    }

    @Override
    public Result selectArtificerTimeByArtificerId(Long artificerId,String artificerDate){
        List<ArtificerTime> artificerTimes = baseMapper.selectList(new QueryWrapper<ArtificerTime>()
                .eq("artificer_id", artificerId)
                .eq("artificer_date", artificerDate));
        for (ArtificerTime artificerTime:artificerTimes){
            if(artificerTime.getClassify()==1){
                //如果是订单占用  判断订单是否完成
                Orders orders = ordersDao.selectById(artificerTime.getOrdersId());
                if(orders==null){
                    artificerTime.setClassify(2);
                    baseMapper.updateById(artificerTime);
                    continue;
                }
                if(orders.getStatus()==3 || orders.getStatus()==5){
                    artificerTime.setClassify(2);
                    baseMapper.updateById(artificerTime);
                }
            }
        }
        return Result.success().put("data", artificerTimes);
    }

    @Override
    public Result selectArtificerTimeListByArtificerId(Long artificerId,String artificerDate){
        List<List<ArtificerTime>> list=new ArrayList<>();
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<5;i++){
            List<ArtificerTime> artificerTimes = baseMapper.selectList(new QueryWrapper<ArtificerTime>().eq("artificer_id", artificerId).eq("artificer_date", artificerDate));
            list.add(artificerTimes);
            calendar.add(Calendar.DAY_OF_MONTH,1);
            artificerDate=simpleDateFormat.format(calendar.getTime());
        }
        return Result.success().put("data",list);
    }



}