package cn.com.tenlion.staff.service.basicstaffinfo.impl;

import cn.com.tenlion.staff.dao.basicstaffinfo.IBasicStaffInfoDao;
import cn.com.tenlion.staff.pojo.dtos.basicstaffinfo.BasicStaffInfoDTO;
import cn.com.tenlion.staff.pojo.vos.basicstaffinfo.BasicStaffInfoVO;
import cn.com.tenlion.staff.service.basicstaffinfo.IBasicStaffInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SaveException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.module.dictionary.pojo.dtos.AreaDTO;
import ink.wgink.module.dictionary.service.IAreaService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.app.AppTokenUser;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 人员基础信息接口实现类
 * @author xwangs
 * @create 2021-04-29 16:10
 * @description
 */
@Service
public class BasicStaffInfoServiceImpl extends DefaultBaseService implements IBasicStaffInfoService {

    @Autowired
    private IBasicStaffInfoDao basicStaffInfoDao;

    @Autowired
    private IAreaService areaService;

    @Override
    public SuccessResultData save(String token, BasicStaffInfoVO basicStaffInfoVO) throws SaveException {
        String basicStaffInfoId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(basicStaffInfoVO);
        params.put("basicStaffInfoId", basicStaffInfoId);
        if (!StringUtils.isEmpty(token)) {
            setAppSaveInfo(token, params);
            params.put("bindUserAccount",params.get("creator").toString());
            params.put("tags","普通用户");
            Map<String, Object> query = new HashMap<>(4);
            query.put("bindUserAccount",params.get("creator").toString());
            BasicStaffInfoDTO dto = basicStaffInfoDao.getById(query);
            // 如果不为空删除后重新保存
            if(dto != null && dto.getBasicStaffInfoId() != null){
                List<String> ids = new ArrayList<>();
                ids.add(dto.getBasicStaffInfoId());
                query.put("ids",ids);
                basicStaffInfoDao.delete(query);
            }
        } else {
            setSaveInfo(params);
        }
        basicStaffInfoDao.save(params);
        return new SuccessResultData(basicStaffInfoId);
    }

    @Override
    public SuccessResult update(String token, String basicStaffInfoId, BasicStaffInfoVO basicStaffInfoVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(basicStaffInfoVO);
        params.put("basicStaffInfoId", basicStaffInfoId);
        if (token != null) {
            setAppUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        basicStaffInfoDao.update(params);
        return new SuccessResult();
    }

    @Override
    public BasicStaffInfoDTO getById(String basicStaffInfoId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("basicStaffInfoId", basicStaffInfoId);
        BasicStaffInfoDTO dto = basicStaffInfoDao.getById(params);
        return dto;
    }

    @Override
    public BasicStaffInfoDTO getBindUserAccount(String token) {
        AppTokenUser appTokenUser = getAppTokenUser(token);
        Map<String, Object> param = getHashMap(8);
        param.put("bindUserAccount",appTokenUser.getId());
        BasicStaffInfoDTO dto = basicStaffInfoDao.getById(param);
        return dto != null ? dto : new BasicStaffInfoDTO();
    }

    @Override
    public List<BasicStaffInfoDTO> list(Map<String, Object> params) throws SearchException {
        return null;
    }

    @Override
    public SuccessResultList<List<BasicStaffInfoDTO>> listPage(ListPage page) throws SearchException {
        //查询字典
        List<Map<String, Object>> dataDTOS = basicStaffInfoDao.dataAll();
        PageHelper.startPage(page.getPage(), page.getRows());
        List<BasicStaffInfoDTO> dtos = basicStaffInfoDao.list(page.getParams());
        for(BasicStaffInfoDTO item :  dtos){
            for(Map<String, Object> dataItem : dataDTOS){
                String dataId = dataItem.get("data_id").toString();
                //性别
                if(dataId.equals(item.getGender())){
                    item.setGender(dataItem.get("data_name").toString());
                }
                //证件类型
                if(dataId.equals(item.getCardType())){
                    item.setCardType(dataItem.get("data_name").toString());
                }
                //民族
                if(dataId.equals(item.getNation())){
                    item.setNation(dataItem.get("data_name").toString());
                }
                //籍贯
                if(dataId.equals(item.getNativePlace())){
                    item.setNativePlace(dataItem.get("data_name").toString());
                }
                //政治面貌
                if(dataId.equals(item.getPoliticalStatus())){
                    item.setPoliticalStatus(dataItem.get("data_name").toString());
                }
                //婚姻状况
                if(dataId.equals(item.getMaritalStatus())){
                    item.setMaritalStatus(dataItem.get("data_name").toString());
                }
                //学历
                if(dataId.equals(item.getEducation())){
                    item.setEducation(dataItem.get("data_name").toString());
                }
                //宗教信仰
                if(dataId.equals(item.getReligion())){
                    item.setReligion(dataItem.get("data_name").toString());
                }
                //职业类别
                if(dataId.equals(item.getOccupationCategory())){
                    item.setOccupationCategory(dataItem.get("data_name").toString());
                }
            }
            //处理籍贯
            String[] nativePlaceStr = item.getNativePlace().split("&");
            if(nativePlaceStr.length > 0 && !"".equals(nativePlaceStr[0])){
                String nativePlace = "";
                AreaDTO area = areaService.get(nativePlaceStr[0]);
                nativePlace += area.getAreaName() + "-";
                AreaDTO area1 = areaService.get(nativePlaceStr[1]);
                nativePlace += area1.getAreaName() + "-";
                AreaDTO area2 = areaService.get(nativePlaceStr[2]);
                nativePlace += area2.getAreaName();
                item.setNativePlace(nativePlace);
            }
            //处理户籍地
            String[] registeredResidenceStr = item.getRegisteredResidence().split("&");
            if(registeredResidenceStr.length > 0 && !"".equals(registeredResidenceStr[0])){
                String registeredResidence = "";
                AreaDTO area = areaService.get(registeredResidenceStr[0]);
                registeredResidence += area.getAreaName() + "-";
                AreaDTO area1 = areaService.get(registeredResidenceStr[1]);
                registeredResidence += area1.getAreaName() + "-";
                AreaDTO area2 = areaService.get(registeredResidenceStr[2]);
                registeredResidence += area2.getAreaName();
                item.setRegisteredResidence(registeredResidence);
            }
            //处理现住地
            String[] currentResidenceStr = item.getCurrentResidence().split("&");
            if(currentResidenceStr.length > 0 && !"".equals(currentResidenceStr[0])){
                String currentResidence = "";
                AreaDTO area = areaService.get(currentResidenceStr[0]);
                currentResidence += area.getAreaName() + "-";
                AreaDTO area1 = areaService.get(currentResidenceStr[1]);
                currentResidence += area1.getAreaName() + "-";
                AreaDTO area2 = areaService.get(currentResidenceStr[2]);
                currentResidence += area2.getAreaName();
                item.setCurrentResidence(currentResidence);
            }
        }
        PageInfo<BasicStaffInfoDTO> pageInfo = new PageInfo<>(dtos);
        return new SuccessResultList<>(dtos, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public SuccessResult remove(String token, String ids) throws RemoveException {
        Map<String, Object> params = getHashMap(4);
        params.put("ids", Arrays.asList(ids.split("_")));
        if (token != null) {
            setAppUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        basicStaffInfoDao.remove(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult delete(String ids) throws RemoveException {
        Map<String, Object> params = getHashMap(4);
        params.put("ids", Arrays.asList(ids.split("_")));
        basicStaffInfoDao.delete(params);
        return new SuccessResult();
    }
}
