package com.zcloud.basic.info.command;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.zcloud.basic.info.domain.gateway.RzUserGateway;
import com.zcloud.basic.info.domain.model.RzUserE;
import com.zcloud.basic.info.dto.RzUserAddCmd;
import com.alibaba.cola.exception.BizException;
import com.zcloud.basic.info.persistence.dataobject.RzUserDO;
import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.gbscommon.utils.UuidUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * web-app
 *
 * @Author SondonYong
 * @Date 2025-12-02 17:15:55
 */
@Component
@AllArgsConstructor
public class RzUserAddExe {
    private final RzUserGateway rzUserGateway;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(RzUserAddCmd cmd) {
        RzUserE rzUserE = new RzUserE();
        BeanUtils.copyProperties(cmd, rzUserE);
        boolean res = false;
        try {
            res = rzUserGateway.add(rzUserE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }

    public void execute(JSONArray jsonArray){
        List<RzUserDO> collect = jsonArray.stream().map(o -> {
            JSONObject json = new JSONObject(o);
            RzUserDO user = new RzUserDO();
            user.setRzUserId(UuidUtil.get32UUID());
            user.setEmployeeName(json.getStr("employee_name"));
            user.setEmployeeGender(json.getStr("employee_gender"));
            user.setEmployeeAge(json.getInt("employee_age"));
            user.setEntryTime(json.getStr("entry_time"));
            user.setEmployeeStatus(json.getStr("employee_status"));
            user.setJobName(json.getStr("job_name"));
            user.setJobLevel(json.getStr("job_level"));
            user.setDeptName(json.getStr("dept_name"));
            user.setDeptCode(json.getStr("dept_code"));
            user.setPositionName(json.getStr("position_name"));
            user.setPhoneNumber(json.getStr("phone_number"));
            user.setIdCardNumber(json.getStr("id_card_number"));
            user.setEmployeeImagesCode(json.getStr("employee_images_code"));
            user.setCorporationCode(json.getStr("corporation_code"));
            user.setCorporationName(json.getStr("corporation_name"));

            // 1、通过姓名或手机号查找用户是否可以对应上
            // 系统中存在姓名为“张三” 但实际保存为“张 三”的情况，需要将此类情况对应上
//            List<PageData> sysUserList = usersService.findByUserNameAndPhone(
//                    json.getStr("employee_name")
//                    , json.getStr("phone_number")
//                    , String.join(" ", json.getStr("employee_name").split("")));
//            String userId = sysUserList.get(0).getString("user_id");
//            // 2、如果对应上，记录用户id以及确认状态
//            if (ObjectUtil.isNotEmpty(sysUserList)) {
//                user.setUSER_ID(userId);
//                user.setIs_confirm("1");
//                user.setConfirm_status("1");
//                PageData pd = new PageData();
//                pd.put("user_id", userId);
//                pd.put("in_hr_user", "1");
//                pd.put("user_id_card", user.getId_card_number());
//                usersService.updateUserIdCard(pd);
//
//            }
            /**
             * 1.寻找用户的图片信息
             * 2.我有用户的头型图片,就不下载了
             *
             */
//            boolean isUpdateImg = true; //是否下载头像图片.
//            if (!Tools.isEmpty(userId)) {
//                List<PageData> userImgesByUserId = usersService.getUserImgesByUserId(userId);
//                if (ObjectUtil.isNotEmpty(userImgesByUserId)) {
//                    isUpdateImg = false;
//                }
//            }
//            if (isUpdateImg) {
//                downloadUserImg(user.getId_card_number(), user.getUSER_ID()); //下载用户图片
//            }
//            long countByIdCardNumber = humanResourceUserService.countByIdCardNumber(user.getId_card_number());
//            if (countByIdCardNumber == 0) {
//                humanResourceUserService.save(user);
//            }
            return user;
        }).collect(Collectors.toList());
    }
}

