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

import com.icomsys.main_vm.biz.common.login.res.UserVo;
import com.icomsys.main_vm.biz.common.system.vo.SystemCustSaveSubReq;
import com.icomsys.main_vm.biz.common.system.vo.SystemServiceGroupUpdateReq;
import com.icomsys.main_vm.db.jpa.entity.system.id.TbServiceGrouopId;
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_SERVICE_GROUP")
@Entity
@Getter
@Setter
@NoArgsConstructor
@DynamicUpdate
@IdClass(TbServiceGrouopId.class)
public class TbServiceGrouop {

    @Id
    @Column(name = "SERVICE_GROUP", nullable = false, length = 20)
    private String serviceGroup;
    @Id
    @Column(name = "SERVICE_GROUP_UPPER", nullable = false, length = 20)
    private String serviceGroupUpper;
    @Id
    @Column(name = "CUST_CODE", nullable = false, length = 20)
    private String custCode;
    @Column(name = "SERVICE_TYPE", nullable = false, length = 1)
    private String serviceType;
    @Column(name = "SERVICE_GROUP_NAME", nullable = false, length = 50)
    private String serviceGroupName;
    @Column(name = "SERVICE_GROUP_DESC", nullable = true, length = 100)
    private String serviceGroupDesc;
    @Column(name = "USE_YN", nullable = false, length = 1)
    private String useYn;
    @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 TbServiceGrouop(String serviceGroup, String serviceGroupUpper, String custCode, String serviceType, String serviceGroupName, String serviceGroupDesc, String useYn, String registId, LocalDateTime registDate, String updateId, LocalDateTime updateDate) {
        this.serviceGroup = serviceGroup;
        this.serviceGroupUpper = serviceGroupUpper;
        this.custCode = custCode;
        this.serviceType = serviceType;
        this.serviceGroupName = serviceGroupName;
        this.serviceGroupDesc = serviceGroupDesc;
        this.useYn = useYn;
        this.registId = registId;
        this.registDate = registDate;
        this.updateId = updateId;
        this.updateDate = updateDate;
    }



    public TbServiceGrouop toUpdateMainService(SystemCustSaveSubReq dto, UserVo uv) {
        if (dto.getAddServiceGroupName() != null && !dto.getAddServiceGroupName().equals("")) {
            this.serviceGroupName = dto.getAddServiceGroupName();
            this.updateId = uv.getUserId();
            this.updateDate = LocalDateTime.now();
        }
        if (dto.getAddServiceGroupDesc() != null && !dto.getAddServiceGroupDesc().equals("")) {
            this.serviceGroupDesc = dto.getAddServiceGroupDesc();
            this.updateId = uv.getUserId();
            this.updateDate = LocalDateTime.now();
        }
        if (dto.getAddUseYn() != null && !dto.getAddUseYn().equals("")) {
            this.useYn = dto.getAddUseYn();
            this.updateId = uv.getUserId();
            this.updateDate = LocalDateTime.now();
        }
        return this;
    }

    public TbServiceGrouop toUpdateSubService(SystemServiceGroupUpdateReq dto, UserVo uv) {
        if (dto.getSubServiceGroupName() != null && !dto.getSubServiceGroupName().equals("") && this.serviceGroupName != dto.getSubServiceGroupName()) {
            this.serviceGroupName = dto.getSubServiceGroupName();
            this.updateId = uv.getUserId();
            this.updateDate = LocalDateTime.now();
        }
        if (dto.getSubServiceGroupDesc() != null && !dto.getSubServiceGroupDesc().equals("") && this.serviceGroupDesc != dto.getSubServiceGroupDesc()) {
            this.serviceGroupDesc = dto.getSubServiceGroupDesc();
            this.updateId = uv.getUserId();
            this.updateDate = LocalDateTime.now();
        }
        if (dto.getSubUseYn() != null && !dto.getSubUseYn().equals("") && this.useYn != dto.getSubUseYn()) {
            this.useYn = dto.getSubUseYn();
            this.updateId = uv.getUserId();
            this.updateDate = LocalDateTime.now();
        }
        return this;

    }

}
