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

import com.alibaba.fastjson.JSON;
import com.boman.common.core.exception.BaseException;
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.Czrk;
import com.boman.domain.CzrkJzdz;
import com.boman.domain.SysUser;
import com.boman.web.core.mapper.CzrkJzdzMapper;
import com.boman.web.core.mapper.CzrkMapper;
import com.boman.web.core.utils.AuthUtils;
import com.boman.web.core.utils.CzrkUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import static com.boman.common.core.utils.obj.ObjectUtils.isEmpty;
import static com.boman.web.core.utils.CzrkUtils.*;

/**
 * 常住人口对应的居住地址Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-14
 */
@Service
@Slf4j
public class CzrkJzdzServiceImpl implements ICzrkJzdzService {

    @Autowired
    private CzrkJzdzMapper czrkJzdzMapper;

    @Resource
    private CzrkMapper czrkMapper;

    /**
     * 查询常住人口对应的居住地址
     *
     * @param id 常住人口对应的居住地址主键
     * @return 常住人口对应的居住地址
     */
    @Override
    public CzrkJzdz selectCzrkJzdzById(Long id) {
        return czrkJzdzMapper.selectCzrkJzdzById(id);
    }

    /**
     * 查询常住人口对应的居住地址列表
     *
     * @param czrkJzdz 常住人口对应的居住地址
     * @return 常住人口对应的居住地址
     */
    @Override
    public List<CzrkJzdz> selectCzrkJzdzList(CzrkJzdz czrkJzdz) {
        return czrkJzdzMapper.selectCzrkJzdzList(czrkJzdz);
    }

    @Override
    public List<String> listIdCard(CzrkJzdz czrkJzdz) {
        return czrkJzdzMapper.listIdCard(czrkJzdz);
    }

    /**
     * 新增常住人口对应的居住地址
     *
     * @param czrkJzdz 常住人口对应的居住地址
     * @return 结果
     */
    @Override
    public int insertCzrkJzdz(CzrkJzdz czrkJzdz) {
        return czrkJzdzMapper.insertCzrkJzdz(czrkJzdz);
    }

    /**
     * 修改常住人口对应的居住地址
     *
     * @param czrkJzdz 常住人口对应的居住地址
     * @return 结果
     */
    @Override
    public int updateCzrkJzdz(CzrkJzdz czrkJzdz) {
        return czrkJzdzMapper.updateCzrkJzdz(czrkJzdz);
    }

    /**
     * 批量删除常住人口对应的居住地址
     *
     * @param ids 需要删除的常住人口对应的居住地址主键
     * @return 结果
     */
    @Override
    public int deleteCzrkJzdzByIds(Long[] ids) {
        return czrkJzdzMapper.deleteCzrkJzdzByIds(ids);
    }

    /**
     * 删除常住人口对应的居住地址信息
     *
     * @param id 常住人口对应的居住地址主键
     * @return 结果
     */
    @Override
    public int deleteCzrkJzdzById(Long id) {
        return czrkJzdzMapper.deleteCzrkJzdzById(id);
    }

    @Override
    public int deleteByCzrkId(Long czrkId) {
        return czrkJzdzMapper.deleteByCzrkId(czrkId);
    }

    @Override
    public List<CzrkJzdz> listByIdCard(List<String> idCardList) {
        if (isEmpty(idCardList)) {
            return Collections.emptyList();
        }

        return czrkJzdzMapper.listByIdCard(idCardList);
    }

    @Override
    public List<CzrkJzdz> listByCzrkIds(List<Long> czrkIds) {
        if (isEmpty(czrkIds)) {
            return Collections.emptyList();
        }

        return czrkJzdzMapper.listByCzrkIds(czrkIds);
    }

    @Override
    public List<Long> listCzrkIdByTypeAreaIdUserName(int type, Long areaId, String userName) {
        return czrkJzdzMapper.listCzrkIdByTypeAreaIdUserName(type, areaId, userName);
    }

    @Override
    public int updateByIdCard(CzrkJzdz czrkJzdz) {
        return czrkJzdzMapper.updateByIdCard(czrkJzdz);
    }


    @Override
    public int updateByCzrkId(CzrkJzdz czrkJzdz) {
        return czrkJzdzMapper.updateByCzrkId(czrkJzdz);
    }

    @Override
    public int updateHouseholdRegister(List<String> idCardList, CzrkJzdz hzCzrkJzdz) {
        return czrkJzdzMapper.updateHouseholdRegister(idCardList, hzCzrkJzdz);
    }

    @Override
    public List<CzrkJzdz> selectCzrkJzdzListByDeptId(List<Long> deptIdList) {
        return czrkJzdzMapper.selectCzrkJzdzListByDeptId(deptIdList);
    }

    @Override
    public List<CzrkJzdz> selectCzrkJzdzListByRegionId(String regionId) {
        return czrkJzdzMapper.selectCzrkJzdzListByRegionId(regionId);
    }

    /**
     * 2022/3/23重新修复的PC人员信息查看常住人口接口
     *
     * @param czrk
     * @return
     */
    @Override
    public List<Czrk> listByCzrkJzdz(Czrk czrk) {
        //todo 获取行政区域id和人员角色,来进行判断查询字段
        SysUser sysUser = AuthUtils.getLoginUser().getSysUser();
        log.info("{}于{}查询常住人员列表，查询条件：{}"
                , sysUser.getUserName(), DateUtils.dateTimeNow(), JSON.toJSONString(czrk));
        setAgeScope(czrk);
        //当前用户只能看到居住地址在当前的人员
        setQueryRole(czrk,sysUser,Czrk.CZ);
        long startTimeSql = System.currentTimeMillis();
        startPage();
        List<Czrk> czrks = czrkMapper.listByCzrkJzdz(czrk);
        long endTimeSql = System.currentTimeMillis() - startTimeSql;
        log.info("执行查询人员信息中常住人员的sql语句时间:" + endTimeSql + "ms");
        packAddr(czrks);
        return czrks;
    }

    @Override
    public List<CzrkJzdz> getCzrkJzdzByTime(String todayStartStr, String todayEndStr, String regionId, String status) {
        return czrkJzdzMapper.getCzrkJzdzByTime(todayStartStr,todayEndStr,regionId,status);
    }

    @Override
    public List<Czrk> exportQianShanCz() {
        List<Czrk> czrkList = czrkMapper.getAllCzrkNoTownCz();

        CzrkUtils.packAddr(czrkList);
        if (czrkList == null || czrkList.size() <= 0) {
            throw new BaseException("请下载属于本乡镇数据");
        }

        return czrkList;
    }

    /**
     * 根据czrdId查询对应的居住地址
     *
     * @param czrkId
     * @return
     */
    @Override
    public List<CzrkJzdz> listByCzrkId(Long czrkId) {
        if (isEmpty(czrkId)) {
            return Collections.emptyList();
        }

        return czrkJzdzMapper.listByCzrkId(czrkId);
    }

    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);
        }
    }
}
