package io.renren.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.business.dao.BusinessPlanTwoDao;
import io.renren.modules.business.entity.BusinessPlanTwoEntity;
import io.renren.modules.business.service.BusinessPlanTwoService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;


@Service("businessPlanTwoService")
public class BusinessPlanTwoServiceImpl extends ServiceImpl<BusinessPlanTwoDao, BusinessPlanTwoEntity> implements BusinessPlanTwoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BusinessPlanTwoEntity> page = this.page(
                new Query<BusinessPlanTwoEntity>().getPage(params),
                new QueryWrapper<BusinessPlanTwoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据时间区间获取列表
     * @param params
     * @return
     */
    @Override
    public List<BusinessPlanTwoEntity> queryList(Map<String, Object> params) {
        return baseMapper.queryList(params);
    }

    @Override
    public BusinessPlanTwoEntity getMaxTime(){
        return baseMapper.getMaxTime();
    }

    /**
     * 新保存  此方法包含  新增和删除需要传入 saveIds 和 deleteIds
     */
    @Override
    public void newSave(Map<String, Object> params) {
        //批量保存
        if(params.get("saveIds") != null&!params.get("saveIds").equals("")){

            String twoEditTime = params.get("twoEditTime").toString();
            String twoStart = params.get("twoStart").toString();
            String twoEnd = params.get("twoEnd").toString();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            String[] str =  params.get("saveIds").toString().split(",");
            BusinessPlanTwoEntity businessPlanTwoEntity = new BusinessPlanTwoEntity();
            for(String i: str){
//                Calendar.getInstance()
                //获取日期+14天  因为是两周计划当天写的计划存在于第二天的开始到2周结束
//                Calendar c = Calendar.getInstance();
//                c.add(Calendar.DAY_OF_MONTH, 14);
//                businessPlanTwoEntity.setTwoEditTime(c.getTime());
                businessPlanTwoEntity.setTwoProjectId(i);
                try {
                    businessPlanTwoEntity.setTwoEditTime(dateFormat.parse(twoEditTime));
                    businessPlanTwoEntity.setTwoStart(dateFormat.parse(twoStart));
                    businessPlanTwoEntity.setTwoEnd(dateFormat.parse(twoEnd));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                businessPlanTwoEntity.setStatus(0);
                baseMapper.insert(businessPlanTwoEntity);
            }
        }
        if(params.get("deleteIds") != null&!params.get("deleteIds").equals("")){
            String[] str =  params.get("deleteIds").toString().split(",");
            for(String i: str){
                baseMapper.deleteById(i);
            }
        }

        /*for(String i : idsA){

         }*/
    }

}