package com.boman.web.core.service.birth;

import com.alibaba.fastjson.JSON;
import com.boman.common.core.utils.DateUtils;
import com.boman.common.core.utils.StringUtils;
import com.boman.common.core.utils.sql.SqlUtil;
import com.boman.common.core.web.page.PageDomain;
import com.boman.common.core.web.page.TableSupport;
import com.boman.domain.SysUser;
import com.boman.domain.ThirdPartInfo;
import com.boman.web.core.domain.BirthRecords;
import com.boman.web.core.mapper.BirthRecordsMapper;
import com.boman.web.core.service.czrk.CzrkServiceImpl;
import com.boman.web.core.utils.AuthUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.boman.web.core.utils.CzrkUtils.setQueryRoleDie;


@Service
@Slf4j
public class BirthRecordsServiceImpl implements BirthRecordsService {

    @Resource
    BirthRecordsMapper birthRecordsMapper;


    @Override
    public void insertListBirthRecords(List<BirthRecords> birthRecordsList) {
        birthRecordsMapper.insertListBirthRecords(birthRecordsList);
    }

    @Override
    public List<BirthRecords> listDie(BirthRecords birthRecords) {
        SysUser sysUser = AuthUtils.getLoginUser().getSysUser();
        log.info("{}于{}查询新生儿人员列表，查询条件：{}"
                , sysUser.getUserName(), DateUtils.dateTimeNow(), JSON.toJSONString(birthRecords));
        //设置权限
        //setQueryRoleDie(thirdPartInfo, sysUser);
        long startTimeSql = System.currentTimeMillis();
        startPage();
        List<BirthRecords> birthRecordsList = birthRecordsMapper.queryList(birthRecords);
        long endTimeSql = System.currentTimeMillis() - startTimeSql;
        log.info("执行新生儿人员sql语句时间:" + endTimeSql + "ms");
        return birthRecordsList;
    }

    private void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
}
