package com.ruoyi.szxc.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.szxc.domain.SzxcHjAddress;
import com.ruoyi.szxc.mapper.SzxcHjAddressMapper;
import com.ruoyi.szxc.service.ISzxcHjAddressService;
import org.springframework.stereotype.Service;

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

/**
 * 户主信息Service业务层处理
 * 
 * @author hs
 * @date 2024-03-15
 */
@Service
public class SzxcHjAddressServiceImpl implements ISzxcHjAddressService 
{
    @Resource
    private SzxcHjAddressMapper szxcHjAddressMapper;

    /**
     * 查询户主信息
     * 
     * @param id 户主信息主键
     * @return 户主信息
     */
    @Override
    public SzxcHjAddress selectSzxcHjAddressById(Long id)
    {
        return szxcHjAddressMapper.selectSzxcHjAddressById(id);
    }

    /**
     * 查询户主信息列表
     * 
     * @param szxcHjAddress 户主信息
     * @return 户主信息
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<SzxcHjAddress> selectSzxcHjAddressList(SzxcHjAddress szxcHjAddress)
    {
        return szxcHjAddressMapper.selectSzxcHjAddressList(szxcHjAddress);
    }

    /**
     * 新增户主信息
     * 
     * @param szxcHjAddress 户主信息
     * @return 结果
     */
    @Override
    public AjaxResult insertSzxcHjAddress(SzxcHjAddress szxcHjAddress)
    {
        //户号重复验证
        SzxcHjAddress hjAddress = new SzxcHjAddress();
        hjAddress.setAcountNo(szxcHjAddress.getAcountNo());
        List<SzxcHjAddress> szxcHjAddresses = szxcHjAddressMapper.selectSzxcHjAddressList(hjAddress);
        if(szxcHjAddresses!=null&&szxcHjAddresses.size()>0){
            return AjaxResult.error("户号已存在");
        }
        //身份证重复验证
        hjAddress.setAcountNo(null);
        hjAddress.setIdCard(szxcHjAddress.getIdCard());
        szxcHjAddresses=szxcHjAddressMapper.selectSzxcHjAddressList(hjAddress);
        if(szxcHjAddresses!=null&&szxcHjAddresses.size()>0){
            return AjaxResult.error("身份证号已存在");
        }
        szxcHjAddress.setCreateTime(DateUtils.getNowDate());
        int result = szxcHjAddressMapper.insertSzxcHjAddress(szxcHjAddress);
        if (result>0){
            return AjaxResult.success("操作成功");
        }
        return AjaxResult.error("操作失败");
    }

    /**
     * 修改户主信息
     * 
     * @param szxcHjAddress 户主信息
     * @return 结果
     */
    @Override
    public AjaxResult updateSzxcHjAddress(SzxcHjAddress szxcHjAddress)
    {
        //户号重复验证
        SzxcHjAddress hjAddress = new SzxcHjAddress();
        hjAddress.setAcountNo(szxcHjAddress.getAcountNo());
        List<SzxcHjAddress> szxcHjAddresses = szxcHjAddressMapper.selectSzxcHjAddressList(hjAddress);
        if(szxcHjAddresses!=null&&szxcHjAddresses.size()>0){
            return AjaxResult.error("户号已存在");
        }
        //身份证重复验证
        hjAddress.setAcountNo(null);
        hjAddress.setIdCard(szxcHjAddress.getIdCard());
        szxcHjAddresses=szxcHjAddressMapper.selectSzxcHjAddressList(hjAddress);
        if(szxcHjAddresses!=null&&szxcHjAddresses.size()>0){
            return AjaxResult.error("身份证号已存在");
        }
        szxcHjAddress.setUpdateTime(DateUtils.getNowDate());
        int result = szxcHjAddressMapper.updateSzxcHjAddress(szxcHjAddress);
        if(result>0){
            return AjaxResult.success("操作成功");
        }
        return AjaxResult.error("操作失败");
    }

    /**
     * 批量删除户主信息
     * 
     * @param ids 需要删除的户主信息主键
     * @return 结果
     */
    @Override
    public int deleteSzxcHjAddressByIds(Long[] ids)
    {
        return szxcHjAddressMapper.deleteSzxcHjAddressByIds(ids);
    }

    /**
     * 删除户主信息信息
     * 
     * @param id 户主信息主键
     * @return 结果
     */
    @Override
    public int deleteSzxcHjAddressById(Long id)
    {
        return szxcHjAddressMapper.deleteSzxcHjAddressById(id);
    }
}
