package com.icomsys.main_vm.db.jpa.entity.system;


import com.icomsys.main_vm.biz.advice.excep.CustomBadRequestException;
import com.icomsys.main_vm.biz.advice.excep.CustomNotFoundException;
import com.icomsys.main_vm.biz.common.login.res.UserVo;
import com.icomsys.main_vm.biz.common.system.vo.SystemCommoncodeUpdateReq;
import com.icomsys.main_vm.biz.rcp.conversation.vo.RetrieveCustNameVo;
import com.icomsys.main_vm.biz.rcp.conversation.vo.TbBotCommonCodeVo;
import com.icomsys.main_vm.db.jpa.entity.system.id.TbBotCommonCodeId;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.time.LocalDateTime;

@Table(name = "TB_BOT_COMMON_CODE")
@Entity
@Getter
@Setter
@NoArgsConstructor
@DynamicUpdate
@IdClass(TbBotCommonCodeId.class)
public class TbBotCommonCode {

    @Id
    @Column(name = "CUST_CODE", nullable = false, length = 20)
    private String custCode;
    @Id
    @Column(name = "MINOR_CODE", nullable = false, length = 20)
    private String minorCode;
    @Id
    @Column(name = "MAJOR_CODE", nullable = false, length = 20)
    private String majorCode;
    @Id
    @Column(name = "OPR_MNG_CODE", nullable = false, length = 40)
    private String oprMngCode;
    @Column(name = "CODE_NAME", nullable = true, length = 100)
    private String codeName;
    @Column(name = "ATTRIBUTE_1", nullable = true, length = 200)
    private String attribute1;
    @Column(name = "ATTRIBUTE_2", nullable = true, length = 200)
    private String attribute2;
    @Column(name = "ATTRIBUTE_3", nullable = true, length = 200)
    private String attribute3;
    @Column(name = "REGIST_ID", nullable = false, length = 20)
    private String registId;
    @Column(name = "REGIST_DATE", nullable = false)
    private LocalDateTime registDate;
    @Column(name = "UPDATE_ID", nullable = true, length = 20)
    private String updateId;
    @Column(name = "UPDATE_DATE", nullable = true)
    private LocalDateTime updateDate;

    @Builder
    public TbBotCommonCode(String custCode, String minorCode, String majorCode, String oprMngCode, String codeName, String attribute1, String attribute2, String attribute3, String registId, LocalDateTime registDate, String updateId, LocalDateTime updateDate) {
        this.custCode = custCode;
        this.minorCode = minorCode;
        this.majorCode = majorCode;
        this.oprMngCode = oprMngCode;
        this.codeName = codeName;
        this.attribute1 = attribute1;
        this.attribute2 = attribute2;
        this.attribute3 = attribute3;
        this.registId = registId;
        this.registDate = registDate;
        this.updateId = updateId;
        this.updateDate = updateDate;
    }

    //Description 가 충돌나서 컨버팅 해주는 메소드
    public TbBotCommonCodeVo toDecs() {
        TbBotCommonCodeVo tbccv = new TbBotCommonCodeVo();
        tbccv.setCustCode(this.custCode);
        tbccv.setMajorCode(this.majorCode);
        tbccv.setMinorCode(this.minorCode);
        tbccv.setOprMngCode(this.oprMngCode);
        tbccv.setCodeName(this.codeName);
        tbccv.setAttribute1(this.attribute1);
        tbccv.setAttribute2(this.attribute2);
        tbccv.setAttribute3(this.attribute3);
        tbccv.setRegistId(this.registId);
        tbccv.setRegistDate(this.registDate);
        tbccv.setUpdateId(this.updateId);
        tbccv.setUpdateDate(this.updateDate);
        return tbccv;
    }

    public RetrieveCustNameVo toRetrieveCustName() {
        RetrieveCustNameVo rcn = new RetrieveCustNameVo();
        rcn.setCodeName(this.codeName);
        rcn.setMinorCode(this.minorCode);
        return rcn;
    }

    public TbBotCommonCode toUpdate(SystemCommoncodeUpdateReq dto, UserVo uv) throws CustomNotFoundException {

        if (dto.getAttribute1().equals(this.attribute1) && dto.getAttribute2().equals(this.attribute2)
                && dto.getAttribute3().equals(this.attribute3) && dto.getCodeName().equals(this.codeName)) {
            throw new CustomNotFoundException();
        } else {
            if (dto.getAttribute1() != null && !dto.getAttribute1().equals("") && dto.getAttribute1() != this.attribute1) {
                this.attribute1 = dto.getAttribute1();
                this.updateId = uv.getUserId();
                this.updateDate = LocalDateTime.now();
            }
            if (dto.getAttribute2() != null && !dto.getAttribute2().equals("") && dto.getAttribute2() != this.attribute2) {
                this.attribute2 = dto.getAttribute2();
                this.updateId = uv.getUserId();
                this.updateDate = LocalDateTime.now();
            }
            if (dto.getAttribute3() != null && !dto.getAttribute3().equals("") && dto.getAttribute3() != this.attribute3) {
                this.attribute3 = dto.getAttribute3();
                this.updateId = uv.getUserId();
                this.updateDate = LocalDateTime.now();
            }
            if (dto.getCodeName() != null && !dto.getCodeName().equals("") && dto.getCodeName() != this.codeName) {
                this.codeName = dto.getCodeName();
                this.updateId = uv.getUserId();
                this.updateDate = LocalDateTime.now();
            }
        }

        return this;
    }

}
