package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.shujuju.Czrk;
import com.ruoyi.system.mapper.shujuju.CzrkMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CreditUserMapper;
import com.ruoyi.system.domain.CreditUser;
import com.ruoyi.system.service.ICreditUserService;

/**
 * 信用人员Service业务层处理
 *
 * @author boman
 * @date 2022-06-10
 */
@Service
@Slf4j
public class CreditUserServiceImpl implements ICreditUserService {


    @Autowired
    private CreditUserMapper creditUserMapper;
    @Autowired
    public CzrkMapper czrkMapper;

    /**
     * 查询信用人员
     *
     * @param id 信用人员主键
     * @return 信用人员
     */
    @Override
    public CreditUser selectCreditUserById(Long id) {
        return creditUserMapper.selectCreditUserById(id);
    }

    /**
     * 查询信用人员
     *
     * @param creditUser
     * @return
     */
    @Override
    public CreditUser selectCreditUser(CreditUser creditUser) {
        return creditUserMapper.selectCreditUser(creditUser);
    }

    /**
     * 查询信用人员列表
     *
     * @param creditUser 信用人员
     * @return 信用人员
     */
    @Override
    public List<CreditUser> selectCreditUserList(CreditUser creditUser) {
        return creditUserMapper.selectCreditUserList(creditUser);
    }

    /**
     * 新增信用人员
     *
     * @param creditUser 信用人员
     * @return 结果
     */
    @Override
    public int insertCreditUser(CreditUser creditUser) {
        creditUser.setCreateTime(DateUtils.getNowDate());
        return creditUserMapper.insertCreditUser(creditUser);
    }

    /**
     * 修改信用人员
     *
     * @param creditUser 信用人员
     * @return 结果
     */
    @Override
    public int updateCreditUser(CreditUser creditUser) {
        creditUser.setUpdateTime(DateUtils.getNowDate());
        return creditUserMapper.updateCreditUser(creditUser);
    }

    /**
     * 批量删除信用人员
     *
     * @param ids 需要删除的信用人员主键
     * @return 结果
     */
    @Override
    public int deleteCreditUserByIds(Long[] ids) {
        return creditUserMapper.deleteCreditUserByIds(ids);
    }

    /**
     * 删除信用人员信息
     *
     * @param id 信用人员主键
     * @return 结果
     */
    @Override
    public int deleteCreditUserById(Long id) {
        return creditUserMapper.deleteCreditUserById(id);
    }

    @Override
    public String importCredit(List<CreditUser> creditUserList) {

        List<String> listName = new ArrayList<>();
        List<CreditUser> creditUserListAll = new ArrayList<>();

        for (CreditUser creditUser : creditUserList) {
            //通过身份证号查出该人员户籍号
            String hjh = czrkMapper.getHjhByIdCard(creditUser.getIdCard());
            if(StringUtils.isNotEmpty(hjh)){
                //通过户籍号查询所有户籍下人员
                List<Czrk> czrkList = czrkMapper.getAllCzrkByHjh(hjh);
                for (Czrk czrk : czrkList) {
                    //通过身份证号查询库里存不存在
                    CreditUser credit = creditUserMapper.selectCreditUserByIdCard(czrk.getIdCard());
                    if(Objects.isNull(credit)){
                        credit = new CreditUser();
                        credit.setUserName(czrk.getUserName());
                        credit.setIdCard(czrk.getIdCard());
                        credit.setCreditLevel(creditUser.getCreditLevel());
                        credit.setCreditScore(creditUser.getCreditScore());
                        credit.setGender(czrk.getGender());
                        credit.setAge(czrk.getAge());
                        credit.setPhoneNum(czrk.getPhoneNum());
                        credit.setProvinceId(czrk.getProvinceId());
                        credit.setProvince(czrk.getProvince());
                        credit.setCityId(czrk.getCity());
                        credit.setCity(czrk.getCity());
                        credit.setRegionId(czrk.getRegionId());
                        credit.setRegion(czrk.getRegion());
                        credit.setVillageTownsId(czrk.getVillageTownsId());
                        credit.setVillageTowns(czrk.getVillageTowns());
                        credit.setVillageId(czrk.getVillageId());
                        credit.setVillage(czrk.getVillage());
                        credit.setVillagerGroupId(czrk.getVillagerGroupId());
                        credit.setVillagerGroup(czrk.getVillagerGroup());
                        credit.setNowIn(czrk.getNowIn());
                        creditUserMapper.insertCreditUser(credit);
                        creditUserListAll.add(credit);
                    }
                }
            }else{
                listName.add(creditUser.getUserName()+"--"+creditUser.getIdCard());
            }
        }

        log.info("未找到的人员信息：{}", listName.toString());

        return "导入成功，以下人员未找到信息"+listName.toString();
    }

    @Override
    public List<CreditUser> getAllCreditUser(CreditUser creditUser) {
        List<CreditUser> creditUserList = creditUserMapper.selectCreditUserList(creditUser);
        return creditUserList;
    }
}
