package com.nantian.cloud.jly.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jly.entity.CheckEnterprice;
import com.nantian.cloud.jly.entity.CheckPlan;
import com.nantian.cloud.jly.mapper.CheckPlanMapper;
import com.nantian.cloud.jly.service.CheckPlanService;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 *
 * @date 2018-09-27
 */
@Service("CheckPlanService")
public class CheckPlanServiceImpl extends ServiceImpl<CheckPlanMapper, CheckPlan> implements CheckPlanService {

//    @Override
//    public IPage<CheckPlan> getByPage(Page page, CheckPlan checkPlan) {
//        String deptId = SecurityUtils.getUser().getDeptId().toString();
//        deptId = deptId.replaceAll("0000","").replaceAll("00","");
//        return baseMapper.getByPage(page,checkPlan,deptId);
//    }

//    @Override
//    public IPage<CheckPlan> getByPage(Page page, CheckPlan checkPlan) {
//        String deptId = SecurityUtils.getUser().getDeptId().toString();
//        deptId = deptId.replaceAll("0000","").replaceAll("00","");
//        List<CheckPlan> checkPlans = baseMapper.getByPage(checkPlan,deptId);
//        //checkPlans.sort(Comparator.comparing(checkPlan.getBeginTime()).reversed());
//        //冒泡排序
//        CheckPlan temp = null;
//        for (int i = checkPlans.size()- 1; i > 0; --i) {
//            for (int j = 0; j < i; ++j) {
//                /**
//                 * 从大到小的排序
//                 */
//                if(checkPlans.get(j+1).getBeginTime().after(checkPlans.get(j).getBeginTime())){
//                    temp = checkPlans.get(j);
//                    checkPlans.set(j, checkPlans.get(j+1));
//                    checkPlans.set(j+1, temp);
//                }else{
//                    /**
//                     * 从小到大排序
//                     */
////            		tempDate = dateList.get(j);
////            		dateList.set(j, dateList.get(j+1));
////            		dateList.set(j+1, tempDate);
//                }
//            }
//        }
//        return page.setRecords(checkPlans);
//    }

    @Override
    public IPage<CheckPlan> getByPage(Page page, CheckPlan checkPlan) {
        String deptId = SecurityUtils.getUser().getDeptId().toString();
        deptId = deptId.replaceAll("0000","").replaceAll("00","");
        IPage iPage = baseMapper.getByPage(page,checkPlan,deptId);
        List<CheckPlan> checkPlans = iPage.getRecords();

        //冒泡排序
        CheckPlan temp = null;
        for (int i = checkPlans.size()- 1; i > 0; --i) {
            for (int j = 0; j < i; ++j) {
                /**
                 * 从大到小的排序
                 */
                if(checkPlans.get(j+1).getBeginTime().after(checkPlans.get(j).getBeginTime())){
                    temp = checkPlans.get(j);
                    checkPlans.set(j, checkPlans.get(j+1));
                    checkPlans.set(j+1, temp);
                }else{
                    /**
                     * 从小到大排序
                     */
//            		tempDate = dateList.get(j);
//            		dateList.set(j, dateList.get(j+1));
//            		dateList.set(j+1, tempDate);
                }
            }
        }
        return iPage.setRecords(checkPlans);
    }

    @Override
    public IPage<CheckPlan> getPageByEpc(Page page, String epc) {
        return baseMapper.getPageByEpc(page,epc);
    }
}

