package com.ruoyi.szxc.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.szxc.domain.SzxcHkbgRecard;
import com.ruoyi.szxc.domain.SzxcJmInfo;
import com.ruoyi.szxc.mapper.SzxcHkbgRecardMapper;
import com.ruoyi.szxc.mapper.SzxcJmInfoMapper;
import com.ruoyi.szxc.service.ISzxcHkbgRecardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 户口变更记录Service业务层处理
 * 
 * @author hs
 * @date 2024-03-15
 */
@Service
public class SzxcHkbgRecardServiceImpl implements ISzxcHkbgRecardService 
{
    @Resource
    private SzxcHkbgRecardMapper szxcHkbgRecardMapper;
    @Resource
    private SzxcJmInfoMapper szxcJmInfoMapper;

    /**
     * 查询户口变更记录
     * 
     * @param id 户口变更记录主键
     * @return 户口变更记录
     */
    @Override
    public SzxcHkbgRecard selectSzxcHkbgRecardById(Long id)
    {
        return szxcHkbgRecardMapper.selectSzxcHkbgRecardById(id);
    }

    /**
     * 查询户口变更记录列表
     * 
     * @param szxcHkbgRecard 户口变更记录
     * @return 户口变更记录
     */
    @Override
    @DataScope(deptAlias = "h")
    public List<SzxcHkbgRecard> selectSzxcHkbgRecardList(SzxcHkbgRecard szxcHkbgRecard)
    {
        return szxcHkbgRecardMapper.selectSzxcHkbgRecardList(szxcHkbgRecard);
    }

    /**
     * 新增户口变更记录
     * 
     * @param szxcHkbgRecard 户口变更记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSzxcHkbgRecard(SzxcHkbgRecard szxcHkbgRecard)
    {
        //先修改居民所属户主信息
        SzxcJmInfo jmInfo = new SzxcJmInfo();
        jmInfo.setAcountNo(szxcHkbgRecard.getAcountNo());
        jmInfo.setDeptId(szxcHkbgRecard.getDeptId());
        jmInfo.setDeptName(szxcHkbgRecard.getDeptName());
        jmInfo.setHjAddress(szxcHkbgRecard.getHjAddress());
        jmInfo.setRelation(szxcHkbgRecard.getRelation());
        jmInfo.setLongLive(szxcHkbgRecard.getLongLive());
        jmInfo.setJgNature(szxcHkbgRecard.getJgNature());
        if(szxcHkbgRecard.getRelation().equals("1")){
            jmInfo.setHead("0");
        }else {
            jmInfo.setHead("1");
        }
        try {
            szxcJmInfoMapper.updateSzxcJmInfo(jmInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        //添加户口变更记录
        szxcHkbgRecard.setCreateTime(DateUtils.getNowDate());
        return szxcHkbgRecardMapper.insertSzxcHkbgRecard(szxcHkbgRecard);
    }

    /**
     * 修改户口变更记录
     * 
     * @param szxcHkbgRecard 户口变更记录
     * @return 结果
     */
    @Override
    public int updateSzxcHkbgRecard(SzxcHkbgRecard szxcHkbgRecard)
    {
        szxcHkbgRecard.setUpdateTime(DateUtils.getNowDate());
        return szxcHkbgRecardMapper.updateSzxcHkbgRecard(szxcHkbgRecard);
    }

    /**
     * 批量删除户口变更记录
     * 
     * @param ids 需要删除的户口变更记录主键
     * @return 结果
     */
    @Override
    public int deleteSzxcHkbgRecardByIds(Long[] ids)
    {
        return szxcHkbgRecardMapper.deleteSzxcHkbgRecardByIds(ids);
    }

    /**
     * 删除户口变更记录信息
     * 
     * @param id 户口变更记录主键
     * @return 结果
     */
    @Override
    public int deleteSzxcHkbgRecardById(Long id)
    {
        return szxcHkbgRecardMapper.deleteSzxcHkbgRecardById(id);
    }
}
