package com.gci.boot.modules.customer.service.impl;

import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.annotation.Query.handle.QueryGenerator;
import com.gci.boot.common.util.ValidateUtil;
import com.gci.boot.common.validated.UpdateValidated;
import com.gci.boot.exception.GciBootException;
import com.gci.boot.modules.customer.entity.TbCustomerTraining;
import com.gci.boot.modules.customer.mapper.TbCustomerTrainingMapper;
import com.gci.boot.modules.customer.query.CustomerTrainingQuery;
import com.gci.boot.modules.customer.service.ITbCustomerTrainingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 客户培训 服务实现类
 * </p>
 *
 * @author 2464
 * @since 2023-10-12
 */
@Service
public class TbCustomerTrainingServiceImpl extends ServiceImpl<TbCustomerTrainingMapper, TbCustomerTraining> implements ITbCustomerTrainingService {

    @Override
    public IPage<TbCustomerTraining> pageBuild(Page<TbCustomerTraining> page, CustomerTrainingQuery query) {
        LambdaQueryWrapper<TbCustomerTraining> lambdaQueryWrapper = QueryGenerator.initQueryWrapper(TbCustomerTraining.class, query).last("order by actual_start_time desc nulls last,create_time desc");
        Page<TbCustomerTraining> list = page(page, lambdaQueryWrapper);
        for (TbCustomerTraining record : list.getRecords()) {
            if (ObjectUtil.isAllNotEmpty(record.getActualStartTime(),record.getActualEndTime())) {
                String s = DateUtil.formatBetween(record.getActualStartTime(), record.getActualEndTime(), BetweenFormatter.Level.MINUTE);
                record.setDuration(s);
            }
        }
        return list;
    }

    @Override
    public void saveItem(TbCustomerTraining tbCustomerTraining) {
        check(tbCustomerTraining);
        save(tbCustomerTraining);
    }

    private void check(TbCustomerTraining tbCustomerTraining) {
        if (ObjectUtil.isAllNotEmpty(tbCustomerTraining.getActualStartTime(), tbCustomerTraining.getActualEndTime())) {
            long between = DateUtil.between(tbCustomerTraining.getActualStartTime(), tbCustomerTraining.getActualEndTime(), DateUnit.MS, false);
            if (between < 0) {
                throw new GciBootException("实际开始时间不能超过实际结束时间！");
            }
        }
    }

    @Override
    public void updateItem(TbCustomerTraining tbCustomerTraining) {
        check(tbCustomerTraining);
        updateById(tbCustomerTraining);
    }

    @Override
    public void saveItemFormContract(TbCustomerTraining tbCustomerTraining) {
        String valid = ValidateUtil.valid(tbCustomerTraining, TbCustomerTraining.FromContract.class);
        if (StrUtil.isNotBlank(valid)) {
            throw new GciBootException(valid);
        }
        check(tbCustomerTraining);
        updateById(tbCustomerTraining);
    }
}
