package com.cirle.scientific.service.impl;

import com.cirle.scientific.dao.QkjMemberAppDao;
import com.cirle.scientific.dto.member.MemberInfoReq;
import com.cirle.scientific.po.QkjMemberApp;
import com.cirle.scientific.service.MemberAppService;
import com.cirle.scientific.utils.OSSUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Objects;

@Service
@Slf4j
@RequiredArgsConstructor
public class MemberAppServiceImpl implements MemberAppService {

    private final QkjMemberAppDao qkjMemberAppDao;


    @Override
    public QkjMemberApp findByMemCard(String memCard) {
        return qkjMemberAppDao.findByMemCard(memCard);
    }

    @Override
    public QkjMemberApp updateUserInfo(MemberInfoReq req) {
        if (Objects.isNull(req) || StringUtils.isBlank(req.getMemCard())) {
            return null;
        }
        String memCard = req.getMemCard();
        QkjMemberApp qkjMember = qkjMemberAppDao.findByMemCard(memCard);
        String memNickname = req.getMemNickname();
        if (StringUtils.isNotBlank(memNickname)) {
            qkjMember.setMemNickname(memNickname);
        }
        String headerImg = req.getMemHeadimg();
        if (StringUtils.isNotBlank(headerImg)) {
            qkjMember.setMemHeadimg(headerImg);
        }

        Integer memSex = req.getMemSex();
        if (Objects.nonNull(memSex)) {
            qkjMember.setMemSex(memSex);
        }
        LocalDate birthday = req.getBirthday();
        if (Objects.nonNull(birthday)) {
            qkjMember.setBirthday(birthday);
        }
        String age = req.getAge();
        if (StringUtils.isNotBlank(age)) {
            qkjMember.setAge(age);
        }
        String unit = req.getUnit();
        if (StringUtils.isNotBlank(unit)) {
            qkjMember.setUnit(unit);
        }
        String department = req.getDepartment();
        if (StringUtils.isNotBlank(department)) {
            qkjMember.setDepartment(department);
        }
        String province = req.getProvince();
        if (StringUtils.isNotBlank(province)) {
            qkjMember.setProvince(province);
        }
        String position = req.getPosition();
        if (StringUtils.isNotBlank(position)) {
            qkjMember.setPosition(position);
        }
        qkjMemberAppDao.save(qkjMember);
        return qkjMember;
    }

    private String getOssHeaderImg(String headerImgUrl, String memCard) {

        log.info("getOssHeaderImg headerImgUrl:{} memCard:{}", headerImgUrl, memCard);
        String key = OSSUtils.getKey(memCard+".jpg");
        String url = OSSUtils.uploadHttpFileNew(headerImgUrl, key);

        return url;
    }
}
