package cn.ey.biz.module.system.service.timesheet;

import cn.ey.biz.common.pojo.PageParam;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.framework.mybatis.core.query.QueryWrapperX;
import cn.ey.biz.module.system.controller.timesheet.approve.vo.TimesheetApproveRuleVO;
import cn.ey.biz.module.system.convert.timesheet.TimesheetApproveRuleConvert;
import cn.ey.biz.module.system.dal.dataobject.timesheet.TimesheetApproveRuleDO;
import cn.ey.biz.module.system.dal.mapper.timesheet.TimesheetApproveRuleMapper;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

@Service
@Log4j2
public class TimesheetApproveRuleServiceImpl
        extends ServiceImpl<TimesheetApproveRuleMapper,TimesheetApproveRuleDO>
        implements TimesheetApproveRuleService {

    @Resource
    private  TimesheetApproveRuleMapper timesheetApproveRuleMapper;

    @Override
    @Transactional
    public void submitApproveRule(List<TimesheetApproveRuleVO> timesheetApproveRuleVOS) {
        remove(new QueryWrapperX<>());
        saveBatch(TimesheetApproveRuleConvert.INSTANCE.convertList(timesheetApproveRuleVOS));
    }

    @Override
    public void deleteTimesheetApprove(Long id) {
        timesheetApproveRuleMapper.deleteById(id);
    }

    @Override
    public List<TimesheetApproveRuleDO> listApproveRule() {
        List<TimesheetApproveRuleDO> list =
                list(new QueryWrapperX<TimesheetApproveRuleDO>().last("limit 100"));
        return CollUtil.emptyIfNull(list);
    }

    @Override
    public PageResult<TimesheetApproveRuleDO> listPageApproveRule(PageParam pageParam) {
        return timesheetApproveRuleMapper.selectPage(pageParam);
    }

    @Override
    public void saveApproveRule(List<TimesheetApproveRuleVO> timesheetApproveRuleVOS) {
        for(TimesheetApproveRuleVO time:timesheetApproveRuleVOS){
            if(time.getApproveRuleId()!=null){
                updateById(TimesheetApproveRuleConvert.INSTANCE.convert(time));
            }else{
                save(TimesheetApproveRuleConvert.INSTANCE.convert(time));
            }
        }
    }

}
