package com.mosty.common.core.business.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.common.base.util.DateUtils;
import com.mosty.common.base.util.StringUtils;
import com.mosty.common.core.business.entity.SysUser;
import com.mosty.common.core.business.entity.TbJcglXfll;
import com.mosty.common.core.business.mapper.TbJcglXfllInCommMapper;
import com.mosty.common.core.business.service.TbJcglXfllInCommService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 巡防力量表;(tb_jcgl_xfll)表服务实现类
 *
 * @author : Lhh
 * @date : 2022-6-4
 */
@AllArgsConstructor
@Service
@DS("jcgl")
public class TbJcglXfllInCommServiceImpl extends ServiceImpl<TbJcglXfllInCommMapper, TbJcglXfll> implements TbJcglXfllInCommService {

    @Override
    public SysUser selectFjToUserByLogin(String userName) {
        if(StringUtils.isBlank(userName)){
            return null;
        }

        //组装查询条件
        QueryWrapper<TbJcglXfll> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("xt_sjzt","1");
        queryWrapper.eq("xt_scbz","0");
        queryWrapper.eq("fl","02");
//        queryWrapper.and(qw -> qw.eq("sfzh", userName).or().likeLeft("sfzh", userName).or().eq("jh", userName));
        queryWrapper.eq("lxdh", userName);

        //查询数据
        List<TbJcglXfll> fjList = this.baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(fjList)){
            return null;
        }

        //组装数据
        return this.fjEntityToSysUser(fjList.get(0));
    }

    @Override
    public SysUser selectFjToUserBySfzh(String sfzh) {
        if(StringUtils.isBlank(sfzh)){
            return null;
        }

        //组装查询条件
        QueryWrapper<TbJcglXfll> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("xt_sjzt","1");
        queryWrapper.eq("xt_scbz","0");
        queryWrapper.eq("fl","02");
        queryWrapper.eq("sfzh", sfzh);

        //查询数据
        List<TbJcglXfll> fjList = this.baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(fjList)){
            return null;
        }

        //组装数据
        return this.fjEntityToSysUser(fjList.get(0));
    }

    /**
     * 辅警实体转为SysUser
     * @param entity 实体
     * @return SysUser
     */
    private SysUser fjEntityToSysUser(TbJcglXfll entity){
        SysUser sysUser = new SysUser();

//        sysUser.setId(entity.get);
        sysUser.setManagerOrgId(StringUtils.isNotBlank(entity.getSsbmid()) ? Long.parseLong(entity.getSsbmid()) : null);
        sysUser.setManagerOrgName(entity.getSsbm());
//        sysUser.setPositionId(entity.get);
//        sysUser.setPositionName(entity.get);
        sysUser.setIsVirtualUser("02");
        sysUser.setLoginName(entity.getSfzh());
        sysUser.setPassword(entity.getPassword());
        sysUser.setUserName(entity.getXm());
        sysUser.setUserType("01");
        sysUser.setEmail(entity.getDzyx());
        sysUser.setMobile(entity.getLxdh());
        sysUser.setTelePhone(entity.getLxdh());
        sysUser.setIdEntityCard(entity.getSfzh());
        sysUser.setInDustRialId(entity.getJh());
        sysUser.setSex(entity.getXbdm());
        sysUser.setNation(entity.getMzdm());
        sysUser.setPolitic(entity.getZzmm());
        sysUser.setMarital(entity.getHyzk());
        sysUser.setType("02");
        sysUser.setTypeName("辅警");
        sysUser.setWhcd(entity.getWhcddm());
        sysUser.setBz(entity.getBz());
        sysUser.setBirthday(entity.getCsrq() != null ? DateUtils.parseDateToStr("yyyy-MM-dd", entity.getCsrq()) : null );
//        sysUser.setBeginTime(entity.getcsrq);
//        sysUser.setEndTime(entity.get);
        sysUser.setSalt(entity.getSalt());
//        sysUser.setXzcs(entity.get);
        sysUser.setXtZxbz(StringUtils.isNotBlank(entity.getXtScbz()) ? Integer.parseInt(entity.getXtScbz()) : null);
//        sysUser.setXtZxyz(entity.get);
        sysUser.setXtCjsj(entity.getXtCjsj());
        sysUser.setXtLrsj(entity.getXtCjsj());
        sysUser.setXtLrrxm(entity.getXtCjr());
        sysUser.setXtLrrid(StringUtils.isNotBlank(entity.getXtCjrId()) ? Long.parseLong(entity.getXtCjrId()) : null);
        sysUser.setXtLrrbm(entity.getXtCjbmmc());
//        sysUser.setXtLrrbmid(entity.get);
        sysUser.setXtLrip(entity.getXtCjip());
        sysUser.setXtZhxgsj(entity.getXtZhgxsj());
        sysUser.setXtZhxgrxm(entity.getXtZhgxr());
        sysUser.setXtZhxgid(StringUtils.isNotBlank(entity.getXtZhgxrid()) ? Long.parseLong(entity.getXtZhgxrid()) : null);
        sysUser.setXtZhxgrbm(entity.getXtZhgxbm());
//        sysUser.setXtZhxgrbmid(entity.get);
        sysUser.setXtZhxgrip(entity.getXtZhgxip());
        sysUser.setSfrh("0");
//        sysUser.setMk();
        sysUser.setAvatar(entity.getTpid());
        sysUser.setXfllId(entity.getId());

        return sysUser;
    }





}
