package com.cirle.scientific.service.impl;
import com.cirle.scientific.dao.*;
import com.cirle.scientific.dto.QkjSignatrueDto;
import com.cirle.scientific.po.QkjSignatrue;
import com.cirle.scientific.pojo.BizCodeEnum;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.SignatrueService;
import com.cirle.scientific.utils.CopyUtil;
import com.cirle.scientific.utils.IdentificationNumberEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


@Service
public class SignatrueServiceImpl implements SignatrueService {

    /**
     * 签名
     */
    @Autowired
    private SignatrueDao signatrueDao;

    /**
     * 解密
     */
    @Autowired
    IdentificationNumberEncryptor identificationNumberEncryptor;


    @Override
    public QkjSignatrueDto selectSignatrue(String memCard,String conferenceId) {
        //获取签名的信息
        QkjSignatrue qkjSignatrue = signatrueDao.selectByMemCard(memCard , conferenceId);
        QkjSignatrueDto qkjSignatrueDto = CopyUtil.copy(qkjSignatrue, QkjSignatrueDto.class);
        if(qkjSignatrue== null){
            qkjSignatrueDto.setIsSubmit(0);
            return qkjSignatrueDto;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if(qkjSignatrue.getSignTime()!=null){
            qkjSignatrueDto.setSignTime(format.format(qkjSignatrue.getSignTime()));
        }
            //解密
        String identityId = identificationNumberEncryptor.decrypt(qkjSignatrue.getIdentityId());
        qkjSignatrueDto.setIdentityId(identityId);
        return qkjSignatrueDto;
    }

    @Override
    public Result addSignatrue(String memCard, String conferenceId, String formId, String partyB, String identityId, String courseMatter, String courseTime, String courseSite, String m, String bankProvince, String bankOfDepositName, String bankId, String bankUserName, String phone, String signImg, Integer isSubmit) {
        QkjSignatrue qkjSignatrue = new QkjSignatrue();
        QkjSignatrue qkjSignatrue1 = signatrueDao.selectByMemCard(memCard ,conferenceId);
        try {
            //如果已经提交,直接返回
            if(qkjSignatrue1 != null && qkjSignatrue1.getIsSubmit()==1){
                return Result.success(BizCodeEnum.SUBMIT_AGAIN);
            }
            if (qkjSignatrue1 != null){
                qkjSignatrue.setId(qkjSignatrue1.getId());
            }
            qkjSignatrue.setMemCard(memCard);
            if(!"null".equals(conferenceId) && !StringUtils.isEmpty(conferenceId)){
                qkjSignatrue.setConferenceId(Integer.valueOf(conferenceId));
            }
            if(!"null".equals(formId) && !StringUtils.isEmpty(formId)){
                qkjSignatrue.setFormId(Integer.valueOf(formId));
            }
            qkjSignatrue.setPartyB(partyB);
            //身份证加密
            qkjSignatrue.setIdentityId(identificationNumberEncryptor.encrypt(identityId));
            qkjSignatrue.setCourseMatter(courseMatter);
            qkjSignatrue.setCourseTime(courseTime);
            qkjSignatrue.setCourseSite(courseSite);
            qkjSignatrue.setM(m);
            qkjSignatrue.setBankProvince(bankProvince);
            qkjSignatrue.setBankOfDepositName(bankOfDepositName);
            qkjSignatrue.setBankId(bankId);
            qkjSignatrue.setBankName(bankUserName);
            qkjSignatrue.setPhone(phone);
            qkjSignatrue.setSignImg(signImg);
            if(signImg !=null && signImg != ""){
                qkjSignatrue.setSignTime(new Date());
            }
            qkjSignatrue.setIsSubmit(isSubmit);
            qkjSignatrue.setStatus(0);
            signatrueDao.save(qkjSignatrue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(BizCodeEnum.SUCCESS);
    }
}
