package com.luowei.app_sb.service;

import com.luowei.app_sb.entity.PersonBaseInfo_881_1Entity;
import com.luowei.app_sb.mapper.PersonBaseInfo_881_1Mapper;
import com.luowei.app_sb.utility.ResponseUtility;
import com.luowei.app_sb.vo.PersonBaseInfo_881_1Vo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PersonBaseInfo_881_1Service {
    @Autowired
    private PersonBaseInfo_881_1Mapper personBaseInfo_881_1Mapper;
    @Autowired
    private ResponseUtility R;

    public List<PersonBaseInfo_881_1Vo> selectAll() {
        List<PersonBaseInfo_881_1Entity> personBaseInfo_881_1EntityList = personBaseInfo_881_1Mapper.selectAll();
        List<PersonBaseInfo_881_1Vo> personBaseInfo_881_1VoList = new ArrayList<>();

        for (PersonBaseInfo_881_1Entity personBaseInfo_881_1Entity : personBaseInfo_881_1EntityList
        ) {
            PersonBaseInfo_881_1Vo personBaseInfo_881_1Vo = new PersonBaseInfo_881_1Vo();
            BeanUtils.copyProperties(personBaseInfo_881_1Entity, personBaseInfo_881_1Vo);
            personBaseInfo_881_1VoList.add(personBaseInfo_881_1Vo);

        }

        return personBaseInfo_881_1VoList;
    }

    public PersonBaseInfo_881_1Vo select(String id) {
        PersonBaseInfo_881_1Entity personBaseInfo_881_1Entity = personBaseInfo_881_1Mapper.select(id);
        PersonBaseInfo_881_1Vo personBaseInfo_881_1Vo = null;
        if (personBaseInfo_881_1Entity != null) {
            personBaseInfo_881_1Vo = new PersonBaseInfo_881_1Vo();
            BeanUtils.copyProperties(personBaseInfo_881_1Entity, personBaseInfo_881_1Vo);
        }

        return personBaseInfo_881_1Vo;
    }


    public ResponseUtility insert(PersonBaseInfo_881_1Vo personBaseInfo_881_1Vo) throws IOException {

        if (personBaseInfo_881_1Mapper.select(personBaseInfo_881_1Vo.getId()) == null) {

            PersonBaseInfo_881_1Entity personBaseInfo_881_1Entity = new PersonBaseInfo_881_1Entity();
            BeanUtils.copyProperties(personBaseInfo_881_1Vo, personBaseInfo_881_1Entity);
            personBaseInfo_881_1Mapper.insert(personBaseInfo_881_1Entity);

            R.setResponseCode("1");
            R.setResponseString("新增数据成功");
            R.setResponseData(personBaseInfo_881_1Vo);
        } else {
            R.setResponseCode("2");
            R.setResponseString("此身份证已存在，新增数据失败");
            R.setResponseData(null);
        }

        return R;
    }

    public ResponseUtility update(PersonBaseInfo_881_1Vo personBaseInfo_881_1Vo) throws IOException {


        PersonBaseInfo_881_1Entity personBaseInfo_881_1Entity = new PersonBaseInfo_881_1Entity();
        BeanUtils.copyProperties(personBaseInfo_881_1Vo, personBaseInfo_881_1Entity);
        personBaseInfo_881_1Mapper.update(personBaseInfo_881_1Entity);

        R.setResponseCode("1");
        R.setResponseString("更新数据成功");
        R.setResponseData(personBaseInfo_881_1Vo);
        return R;
    }

    public ResponseUtility delete(String id) {
        personBaseInfo_881_1Mapper.delete(id);
        R.setResponseCode("1");
        R.setResponseString("删除数据成功");
        R.setResponseData(null);
        return R;
    }
}
