package repository

import (
	"bpmsapi/dto"
	"bpmsapi/entity"
	"bytes"
	"database/sql"
	"errors"
	"fmt"
	"log"
	"strconv"
	"strings"

	"github.com/jmoiron/sqlx"
)

type ServiceRequestActions interface { //entity - service reponse
	CreateServiceRequest(input entity.CreateRequestInput) (string, error)
	FetchOpenSRQByUser(userid int, apartmentid int) ([]entity.ServiceRequest, error)
	GetProblemName(input entity.GetProblemDescriptionInput) ([]entity.GetProblemDescription, error)
	ServiceRequestFetch(input entity.FetchServiceRequestInput) (*entity.FetchServiceRequest, error)
	UpdateServiceRequest(tx *sqlx.Tx, User_Id int, Srq_Id int, Status string, Srq_Problem_Desc string, Srq_Visit_Timeslot string, img_paths []string) (srqList []entity.ServiceRequest, err error)
}

type ServiceRequestRepository struct {
	db *sqlx.DB
}

func ServiceRequestRepositoryInit(db *sqlx.DB) *ServiceRequestRepository {
	return &ServiceRequestRepository{
		db: db,
	}
}

func (srqRepo *ServiceRequestRepository) Begin() (*sqlx.Tx, error) {
	tx, err := srqRepo.db.Beginx()
	if err != nil {
		return nil, err
	}
	return tx, nil
}

func (srqRepo *ServiceRequestRepository) CreateServiceRequest(tx *sqlx.Tx, input entity.CreateRequestInput, img_paths []string) (*entity.ServiceRequest, error) {

	var lastInsertId int64
	var inserr error

	query := "INSERT INTO `BPMS_SERVICE_REQUEST`( `Srq_Service`, `Request_Type`, `Srq_Apartment_Id`, `User_Id`, `Srq_Tax_Percentage`, `Srq_Stage_1`, `Srq_Stage_1_ts`, `Srq_Problem_Desc`, `Srq_Problem_Category`, `Srq_Category_others`, `Srq_Visit_Timeslot`, `Srq_Instructions`, `Created_Date`) VALUES (:Srq_Service, :Request_Type, :Srq_Apartment_Id, :User_Id , :Srq_Tax_Percentage , 1,CURRENT_TIMESTAMP(), :Srq_Problem_Desc, :Srq_Problem_Category, :Srq_Category_others, :Srq_Visit_Timeslot, :Srq_Instructions, CURRENT_TIMESTAMP())"
	insert, err := tx.NamedExec(query, input)
	if err != nil {
		tx.Rollback()
		log.Println("ServiceRepository: CreateServiceRequest: creatingsrq: input: ", input.User_Id, " Error: ", err.Error())
		return nil, err
	}

	lastInsertId, inserr = insert.LastInsertId()
	if inserr != nil {
		tx.Rollback()
		log.Println("ServiceRepository: CreateServiceRequest: gettingSRQId: Error: ", inserr.Error())
		return nil, inserr
	}

	newkey := fmt.Sprintf("%d", lastInsertId)
	newKeyId := "SRQ-" + newkey

	keyQuery := "UPDATE BPMS_SERVICE_REQUEST SET Srq_Key=? WHERE Srq_Id =?"
	res, errIn := tx.Exec(keyQuery, newKeyId, lastInsertId)
	if errIn != nil {
		tx.Rollback()
		log.Println("ServiceRepository: CreateServiceRequest: updatingKey: input: ", lastInsertId, " Error: ", errIn.Error())
		return nil, errIn
	}

	rowsAffected, _ := res.RowsAffected()
	if rowsAffected == 0 {
		tx.Rollback()
		log.Println("ServiceRepository: CreateServiceRequest: updatingKey: rowsaffected: userid: ", input.User_Id)
		return nil, errors.New(dto.ErrorrowsAffectedSRQCreate)
	}
	//image
	if len(img_paths) > 0 {
		for _, img_path := range img_paths {
			imgQuery := "Insert INTO `BPMS_SRQ_IMAGE` (`Image_Path`,`Srq_Id`) values (?,?)"
			_, errImg := tx.Exec(imgQuery, img_path, lastInsertId)
			if errImg != nil {
				tx.Rollback()
				log.Println("ServiceRepository: CreateServiceRequest: insertingimage: input: ", lastInsertId, " Error: ", errImg.Error())
				return nil, errImg
			}

		}

	}
	//promocode
	log.Println("ServiceRepository: CreateServiceRequest: promocode: ", input.Promo_Id, "srq_id :", input.Srq_Apartment_Id)
	if input.Promo_Id != nil && *input.Promo_Id != 0 {
		promoQuery := "UPDATE BPMS_PROMO_MAPPING SET Is_Used = 1, Is_Active = 0, Srq_Id = ? Where User_Id = ? AND Is_Used =0 AND Promocode_Id = ?"
		res, errIn1 := tx.Exec(promoQuery, lastInsertId, input.User_Id, input.Promo_Id)
		if errIn1 != nil {
			tx.Rollback()
			log.Println("ServiceRepository: CreateServiceRequest: updatingpromocode: srq_id: ", lastInsertId, "Promoid:", input.Promo_Id, " Error: ", errIn1.Error())
			return nil, errIn1
		}
		promoRowsAffected, _ := res.RowsAffected()
		if promoRowsAffected == 0 {
			tx.Rollback()
			log.Println("ServiceRepository: CreateServiceRequest: :promoid: ", input.Promo_Id, " :rowsaffected")
			return nil, errors.New(dto.ErrorrowsAffectedSRQCreatePromo)
		}
	}
	//other category
	if input.Srq_Category_Others != "" {
		othersQuery := "UPDATE BPMS_SERVICE_REQUEST SET Srq_Problem_Category=0 WHERE Srq_Id =?"
		_, errIns := tx.Exec(othersQuery, lastInsertId)
		if errIns != nil {
			tx.Rollback()
			log.Println("ServiceRepository: CreateServiceRequest: othercategory: input: srq_id", lastInsertId, " Error: ", errIns.Error())
			return nil, errIns
		}

		// rowsAffectedOthers, _ := resIns.RowsAffected()
		// if rowsAffectedOthers == 0 {
		// 	tx.Rollback()
		// 	log.Println("ServiceRepository: CreateServiceRequest: othercategory : rowsaffected: srqid:", lastInsertId)
		// 	return nil, errors.New(dto.ErrorrowsAffectedSRQCreate)
		// }
	}
	requestResponse := entity.ServiceRequest{}
	getQuery := tx.Get(&requestResponse, "SELECT bsr.Srq_Id, bsr.Srq_Key, bsr.Srq_Service, bst.Service_Name, bs.Service_Description, bs.Service_Image_Large, bs.Service_Image_Small, bsr.Request_Type, bsr.Parent_Request, bsr.Child_Request, bsr.Srq_Apartment_Id, bat.Apartment_Name,ba.Apartment_Image, bsat.State_Name, bct.City_Name, bsr.User_Id, bu.User_Name, bu.User_Mobno, bu.User_Image, buam.Block, buam.Door_No, bsr.Adm_Id, be.Emp_Name AS Admin_Name, be.Emp_Mobno AS Admin_Mobno, bsr.Admin_Comments, bsr.Technician_Id, bet.Emp_Name As Technician_Name, bet.Emp_Mobno AS Technician_Mobno, bet.Emp_Image AS Tech_Image, bsr.Technician_Comments, bpm.Promocode_Id, bp.Name, bp.Discount, bp.Max_Discount, bsr.Srq_Service_Cost,bsr.Srq_Material_Cost, bsr.Srq_Labour_Cost, bsr.Srq_Tax_Percentage, bsr.Srq_Tax, bsr.Srq_Discount_Amount, bsr.Srq_Other_Cost, bsr.Srq_Special_Discount, bsr.Srq_Final_Cost, bsr.Quote_Service_Cost,bsr.Quote_Labour_Cost, bsr.Quote_Material_Cost, bsr.Quote_Tax, bsr.Quote_Other_Cost, bsr.Srq_Quote_Cost, bsr.Srq_Stage_1, bsr.Srq_Stage_1_ts, bsr.Srq_Stage_2, bsr.Srq_Stage_2_ts, bsr.Srq_Stage_3, bsr.Srq_Stage_3_ts, bsr.Srq_Stage_4, bsr.Srq_Stage_4_ts, bsr.Srq_Stage_5, bsr.Srq_Stage_5_ts, bsr.Srq_Stage_6, bsr.Srq_Stage_6_ts, bsr.Srq_Stage_7, bsr.Srq_Stage_7_ts, bsr.Srq_Stage_8, bsr.Srq_Stage_8_ts, bsr.Srq_Stage_9, bsr.Srq_Stage_9_ts, bsr.Is_Happycode_Validated, bsr.Srq_Payment_Mode, bsr.Srq_Payment_Status, bsr.Srq_Quote_Status, bsr.Srq_Problem_Desc, bsr.Srq_Problem_Category,bpct.Problem_Name, bsr.Srq_Category_others, bsr.Srq_Visit_Timeslot, bsr.Srq_Instructions, bsr.Created_Date, bf.Feedback, bf.Rating FROM BPMS_SERVICE_REQUEST bsr INNER JOIN BPMS_SERVICE_TRANSLATIONS bst ON bsr.Srq_Service = bst.Service_Id And bst.Language_Code =? INNER JOIN BPMS_SERVICE bs ON bsr.Srq_Service = bs.Service_Id LEFT JOIN BPMS_APARTMENT ba ON bsr.Srq_Apartment_Id = ba.Apartment_Id INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON bsr.Srq_Apartment_Id = bat.Apartment_Id AND bat.Language_Code =? INNER JOIN BPMS_STATE_TRANSLATIONS bsat ON ba.State = bsat.State_Id And bsat.Language_Code =? INNER JOIN BPMS_CITY bc ON ba.State = bc.State_Id INNER JOIN BPMS_CITY_TRANSLATIONS bct ON bc.City_Id = bct.City_Id AND bct.Language_Code =? INNER JOIN BPMS_USER bu ON bsr.User_Id = bu.User_Id LEFT JOIN BPMS_PROMO_MAPPING bpm ON bsr.Srq_Id = bpm.Srq_Id LEFT JOIN BPMS_PROMOCODE bp ON bpm.Promocode_Id = bp.Promocode_Id LEFT JOIN BPMS_PROBLEM_CATEGORIES_TRANSLATIONS bpct ON bsr.Srq_Problem_Category = bpct.Problem_Id AND bpct.Language_Code =? LEFT JOIN BPMS_FEEDBACK bf ON bsr.Srq_Id = bf.Service_Id LEFT JOIN BPMS_EMPLOYEE be ON bsr.Adm_Id = be.Emp_Id LEFT JOIN BPMS_EMPLOYEE bet ON bsr.Technician_Id = bet.Emp_Id INNER JOIN BPMS_USER_APARTMENT_MAPPING buam ON bsr.Srq_Apartment_Id = buam.Apartment_Id AND bsr.User_Id = buam.User_Id WHERE bsr.Srq_Id=?", input.Language_Code, input.Language_Code, input.Language_Code, input.Language_Code, input.Language_Code, lastInsertId)
	requestResponse.SrqImages, _ = srqRepo.FetchServiceRequestImage(tx, lastInsertId)
	if getQuery != nil {
		log.Println("ServiceRepository: CreateServiceRequest: gettingsrqdetails: input: ", lastInsertId, " Error: ", getQuery.Error())
		return nil, getQuery
	} else {
		return &requestResponse, nil
	}
}
func (srqRepo *ServiceRequestRepository) FetchServiceRequestImage(tx *sqlx.Tx, lastInsertId int64) (srqimages []entity.SrqImage, err error) {
	err = tx.Select(&srqimages, "SELECT Image_Path from BPMS_SRQ_IMAGE WHERE Srq_Id =?", int(lastInsertId))
	if err != nil {
		log.Print("ServiceRepository: FetchServiceRequestImage: fetchingImages: Input: Srq_Id: ", lastInsertId, " Error: ", err.Error())
		return nil, err
	} else {
		return srqimages, nil
	}
}
func (serviceRepo *ServiceRequestRepository) FetchOpenSRQByUser(userid int, apartmentid int, languagecode int) (services []entity.ServiceRequest, err error) {
	err = serviceRepo.db.Select(&services, "SELECT bsr.Srq_Id, bsr.Srq_Key, bsr.Srq_Service, bst.Service_Name, bs.Service_Description, bs.Service_Image_Large, bs.Service_Image_Small, bsr.Request_Type, bsr.Parent_Request, bsr.Child_Request, bsr.Srq_Apartment_Id, bat.Apartment_Name,ba.Apartment_Image, bsat.State_Name, bct.City_Name, bsr.User_Id, bu.User_Name, bu.User_Mobno, bu.User_Image, buam.Block, buam.Door_No, bsr.Adm_Id, be.Emp_Name AS Admin_Name, be.Emp_Mobno AS Admin_Mobno, bsr.Admin_Comments, bsr.Technician_Id, bet.Emp_Name As Technician_Name, bet.Emp_Mobno AS Technician_Mobno, bet.Emp_Image AS Tech_Image, bsr.Technician_Comments, bpm.Promocode_Id, bp.Name, bp.Discount, bp.Max_Discount, bsr.Srq_Service_Cost,bsr.Srq_Material_Cost, bsr.Srq_Labour_Cost, bsr.Srq_Tax_Percentage, bsr.Srq_Tax, bsr.Srq_Discount_Amount, bsr.Srq_Other_Cost, bsr.Srq_Special_Discount, bsr.Srq_Final_Cost, bsr.Quote_Service_Cost,bsr.Quote_Labour_Cost, bsr.Quote_Material_Cost, bsr.Quote_Tax, bsr.Quote_Other_Cost, bsr.Srq_Quote_Cost, bsr.Srq_Stage_1, bsr.Srq_Stage_1_ts, bsr.Srq_Stage_2, bsr.Srq_Stage_2_ts, bsr.Srq_Stage_3, bsr.Srq_Stage_3_ts, bsr.Srq_Stage_4, bsr.Srq_Stage_4_ts, bsr.Srq_Stage_5, bsr.Srq_Stage_5_ts, bsr.Srq_Stage_6, bsr.Srq_Stage_6_ts, bsr.Srq_Stage_7, bsr.Srq_Stage_7_ts, bsr.Srq_Stage_8, bsr.Srq_Stage_8_ts, bsr.Srq_Stage_9, bsr.Srq_Stage_9_ts, bsr.Is_Happycode_Validated, bsr.Srq_Payment_Mode, bsr.Srq_Payment_Status, bsr.Srq_Quote_Status, bsr.Srq_Problem_Desc, bsr.Srq_Problem_Category,bpct.Problem_Name, bsr.Srq_Category_others,GROUP_CONCAT(bsi.Image_Path) As SRQ_Image, bsr.Srq_Visit_Timeslot, bsr.Srq_Instructions, bsr.Created_Date, bf.Feedback, bf.Rating FROM BPMS_SERVICE_REQUEST bsr INNER JOIN BPMS_SERVICE_TRANSLATIONS bst ON bsr.Srq_Service = bst.Service_Id And bst.Language_Code =? INNER JOIN BPMS_SERVICE bs ON bsr.Srq_Service = bs.Service_Id LEFT JOIN BPMS_APARTMENT ba ON bsr.Srq_Apartment_Id = ba.Apartment_Id INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON bsr.Srq_Apartment_Id = bat.Apartment_Id AND bat.Language_Code =? INNER JOIN BPMS_STATE_TRANSLATIONS bsat ON ba.State = bsat.State_Id And bsat.Language_Code =? INNER JOIN BPMS_CITY bc ON ba.State = bc.State_Id INNER JOIN BPMS_CITY_TRANSLATIONS bct ON bc.City_Id = bct.City_Id AND bct.Language_Code =? INNER JOIN BPMS_USER bu ON bsr.User_Id = bu.User_Id LEFT JOIN BPMS_PROMO_MAPPING bpm ON bsr.Srq_Id = bpm.Srq_Id LEFT JOIN BPMS_PROMOCODE bp ON bpm.Promocode_Id = bp.Promocode_Id LEFT JOIN BPMS_PROBLEM_CATEGORIES_TRANSLATIONS bpct ON bsr.Srq_Problem_Category = bpct.Problem_Id AND bpct.Language_Code =? LEFT JOIN BPMS_FEEDBACK bf ON bsr.Srq_Id = bf.Service_Id LEFT JOIN BPMS_EMPLOYEE be ON bsr.Adm_Id = be.Emp_Id LEFT JOIN BPMS_EMPLOYEE bet ON bsr.Technician_Id = bet.Emp_Id LEFT JOIN BPMS_SRQ_IMAGE bsi ON bsr.Srq_Id = bsi.Srq_Id INNER JOIN BPMS_USER_APARTMENT_MAPPING buam ON bsr.Srq_Apartment_Id = buam.Apartment_Id AND bsr.User_Id = buam.User_Id WHERE bsr.User_Id = ? AND bsr.Srq_Apartment_Id = ? AND bsr.Srq_Stage_7 = 0 AND bsr.Srq_Stage_8 = 0 GROUP BY bsr.Srq_Id ORDER BY bsr.Srq_Id DESC", languagecode, languagecode, languagecode, languagecode, languagecode, userid, apartmentid)
	if err != nil {
		return nil, err
	} else {
		return services, nil
	}
}

// func (serviceRepo *ServiceRequestRepository) FetchOpenSRQByUser(userid int, apartmentid int) (services []entity.ServiceRequest, err error) {
// 	err = serviceRepo.db.Select(&services, "SELECT s.Srq_Id, s.Srq_Key, s.Srq_Service, s.Request_Type, s.Srq_Apartment_Id, s.User_Id, s.Adm_Id, s.Technician_Id, s.Srq_Service_Cost, s.Srq_Final_Cost, s.Srq_Quote_Cost, s.Srq_Stage_1, s.Srq_Stage_1_ts, s.Srq_Stage_2, s.Srq_Stage_2_ts, s.Srq_Stage_3, s.Srq_Stage_3_ts, s.Srq_Stage_4, s.Srq_Stage_4_ts, s.Srq_Stage_5, s.Srq_Stage_5_ts, s.Srq_Stage_6, s.Srq_Stage_6_ts, s.Srq_Stage_7, s.Srq_Stage_7_ts, s.Srq_Stage_8, s.Srq_Stage_8_ts, s.Srq_Stage_9, s.Srq_Stage_9_ts, s.Srq_Payment_Status, s.Srq_Quote_Status, s.Srq_Problem_Desc, s.Srq_Problem_Category, s.Srq_Category_others, s.Srq_Visit_Timeslot, s.Srq_Instructions, s.Created_Date, bf.Feedback, bf.Rating, GROUP_CONCAT(bi.Image_path) as SRQ_Image  FROM BPMS_SERVICE_REQUEST s LEFT OUTER JOIN BPMS_SRQ_IMAGE bi on bi.Srq_Id = s.Srq_Id LEFT OUTER JOIN BPMS_FEEDBACK bf on (bf.Service_ID = s.Srq_Service AND bf.FeedBack_Type = 1) WHERE s.User_Id = ? AND s.Srq_Apartment_Id = ? AND s.Srq_Stage_7 = 0 AND s.Srq_Stage_8 = 0 GROUP BY s.Srq_Id ORDER BY s.Srq_Id DESC", userid, apartmentid)
// 	if err != nil {
// 		return nil, err
// 	} else {
// 		return services, nil
// 	}
// }

func (serviceRepo *ServiceRequestRepository) GetProblemName(input entity.GetProblemDescriptionInput) (problem_name []entity.GetProblemDescription, err error) {

	err = serviceRepo.db.Select(&problem_name, "SELECT bpct.Problem_Name FROM BPMS_PROBLEM_CATEGORIES_TRANSLATIONS bpct INNER JOIN BPMS_PROBLEM_CATEGORIES bpc ON bpct.Problem_Id = bpc.Problem_Id WHERE bpc.Service_Id = ? AND bpct.Language_Code = ?", input.Service_Id, input.Language_Code)
	if err != nil {
		log.Println("ServiceRepository: ProblemCategorySearch: gettingprblmdescription: Input: ", input.Service_Id, " Error: ", err.Error())
		return nil, err
	} else {
		return problem_name, nil
	}

}

func (serviceRepo *ServiceRequestRepository) ServiceRequestFetch(input entity.FetchServiceRequestInput) (_ *entity.FetchServiceRequest, err error) {
	var pageLimit int = 15
	var srqQuery, countQuery, costQuery bytes.Buffer
	srqQuery.WriteString("SELECT bsr.Srq_Id, bsr.Srq_Key, bsr.Srq_Service, bst.Service_Name, bs.Service_Description, bs.Service_Image_Large, bs.Service_Image_Small, bsr.Request_Type, bsr.Parent_Request, bsr.Child_Request, bsr.Srq_Apartment_Id, bat.Apartment_Name,ba.Apartment_Image, bsat.State_Name, bct.City_Name, bsr.User_Id, bu.User_Name, bu.User_Mobno, bu.User_Image, buam.Block, buam.Door_No, bsr.Adm_Id, be.Emp_Name AS Admin_Name, be.Emp_Mobno AS Admin_Mobno, bsr.Admin_Comments, bsr.Technician_Id, bet.Emp_Name As Technician_Name, bet.Emp_Mobno AS Technician_Mobno, bet.Emp_Image AS Tech_Image, bsr.Technician_Comments, bpm.Promocode_Id, bp.Name, bp.Discount, bp.Max_Discount, bsr.Srq_Service_Cost,bsr.Srq_Material_Cost, bsr.Srq_Labour_Cost, bsr.Srq_Tax_Percentage, bsr.Srq_Tax, bsr.Srq_Discount_Amount, bsr.Srq_Other_Cost, bsr.Srq_Special_Discount, bsr.Srq_Final_Cost, bsr.Quote_Service_Cost,bsr.Quote_Labour_Cost, bsr.Quote_Material_Cost,  bsr.Quote_Tax, bsr.Quote_Other_Cost, bsr.Srq_Quote_Cost, bsr.Srq_Stage_1, bsr.Srq_Stage_1_ts, bsr.Srq_Stage_2, bsr.Srq_Stage_2_ts, bsr.Srq_Stage_3, bsr.Srq_Stage_3_ts, bsr.Srq_Stage_4, bsr.Srq_Stage_4_ts, bsr.Srq_Stage_5, bsr.Srq_Stage_5_ts, bsr.Srq_Stage_6, bsr.Srq_Stage_6_ts, bsr.Srq_Stage_7, bsr.Srq_Stage_7_ts, bsr.Srq_Stage_8, bsr.Srq_Stage_8_ts, bsr.Srq_Stage_9, bsr.Srq_Stage_9_ts, bsr.Is_Happycode_Validated, bsr.Srq_Payment_Mode, bsr.Srq_Payment_Status, bsr.Srq_Quote_Status, bsr.Srq_Problem_Desc, bsr.Srq_Problem_Category,bpct.Problem_Name, bsr.Srq_Category_others,GROUP_CONCAT(bsi.Image_Path) As SRQ_Image, bsr.Srq_Visit_Timeslot, bsr.Srq_Instructions, bsr.Created_Date, bf.Feedback, bf.Rating FROM BPMS_SERVICE_REQUEST bsr INNER JOIN BPMS_SERVICE_TRANSLATIONS bst ON bsr.Srq_Service = bst.Service_Id And bst.Language_Code =? INNER JOIN BPMS_SERVICE bs ON bsr.Srq_Service = bs.Service_Id LEFT JOIN BPMS_APARTMENT ba ON bsr.Srq_Apartment_Id = ba.Apartment_Id INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON bsr.Srq_Apartment_Id = bat.Apartment_Id AND bat.Language_Code =? INNER JOIN BPMS_STATE_TRANSLATIONS bsat ON ba.State = bsat.State_Id And bsat.Language_Code =? INNER JOIN BPMS_CITY bc ON ba.State = bc.State_Id INNER JOIN BPMS_CITY_TRANSLATIONS bct ON bc.City_Id = bct.City_Id AND bct.Language_Code =? INNER JOIN BPMS_USER bu ON bsr.User_Id = bu.User_Id LEFT JOIN BPMS_PROMO_MAPPING bpm ON bsr.Srq_Id = bpm.Srq_Id LEFT JOIN BPMS_PROMOCODE bp ON bpm.Promocode_Id = bp.Promocode_Id LEFT JOIN BPMS_PROBLEM_CATEGORIES_TRANSLATIONS bpct ON bsr.Srq_Problem_Category = bpct.Problem_Id AND bpct.Language_Code =? LEFT JOIN BPMS_FEEDBACK bf ON bsr.Srq_Id = bf.Service_Id LEFT JOIN BPMS_EMPLOYEE be ON bsr.Adm_Id = be.Emp_Id LEFT JOIN BPMS_EMPLOYEE bet ON bsr.Technician_Id = bet.Emp_Id LEFT JOIN BPMS_SRQ_IMAGE bsi ON bsr.Srq_Id = bsi.Srq_Id INNER JOIN BPMS_USER_APARTMENT_MAPPING buam ON bsr.Srq_Apartment_Id = buam.Apartment_Id AND bsr.User_Id = buam.User_Id")

	countQuery.WriteString("SELECT COUNT(DISTINCT bsr.Srq_Id) FROM BPMS_SERVICE_REQUEST bsr INNER JOIN BPMS_SERVICE_TRANSLATIONS bst ON bsr.Srq_Service = bst.Service_Id And bst.Language_Code =? INNER JOIN BPMS_SERVICE bs ON bsr.Srq_Service = bs.Service_Id LEFT JOIN BPMS_APARTMENT ba ON bsr.Srq_Apartment_Id = ba.Apartment_Id INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON bsr.Srq_Apartment_Id = bat.Apartment_Id AND bat.Language_Code =? INNER JOIN BPMS_STATE_TRANSLATIONS bsat ON ba.State = bsat.State_Id And bsat.Language_Code =? INNER JOIN BPMS_CITY bc ON ba.State = bc.State_Id INNER JOIN BPMS_CITY_TRANSLATIONS bct ON bc.City_Id = bct.City_Id AND bct.Language_Code =? INNER JOIN BPMS_USER bu ON bsr.User_Id = bu.User_Id LEFT JOIN BPMS_PROMO_MAPPING bpm ON bsr.Srq_Id = bpm.Srq_Id LEFT JOIN BPMS_PROMOCODE bp ON bpm.Promocode_Id = bp.Promocode_Id LEFT JOIN BPMS_PROBLEM_CATEGORIES_TRANSLATIONS bpct ON bsr.Srq_Problem_Category = bpct.Problem_Id AND bpct.Language_Code =? LEFT JOIN BPMS_FEEDBACK bf ON bsr.Srq_Id = bf.Service_Id LEFT JOIN BPMS_EMPLOYEE be ON bsr.Adm_Id = be.Emp_Id LEFT JOIN BPMS_EMPLOYEE bet ON bsr.Technician_Id = bet.Emp_Id ")

	costQuery.WriteString("SELECT SUM(sub.Srq_Final_Cost) FROM (SELECT DISTINCT bsr.Srq_Id, bsr.Srq_Final_Cost FROM BPMS_SERVICE_REQUEST bsr INNER JOIN BPMS_SERVICE_TRANSLATIONS bst ON bsr.Srq_Service = bst.Service_Id And bst.Language_Code =? INNER JOIN BPMS_SERVICE bs ON bsr.Srq_Service = bs.Service_Id LEFT JOIN BPMS_APARTMENT ba ON bsr.Srq_Apartment_Id = ba.Apartment_Id INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON bsr.Srq_Apartment_Id = bat.Apartment_Id AND bat.Language_Code =? INNER JOIN BPMS_STATE_TRANSLATIONS bsat ON ba.State = bsat.State_Id And bsat.Language_Code =? INNER JOIN BPMS_CITY bc ON ba.State = bc.State_Id INNER JOIN BPMS_CITY_TRANSLATIONS bct ON bc.City_Id = bct.City_Id AND bct.Language_Code =? INNER JOIN BPMS_USER bu ON bsr.User_Id = bu.User_Id LEFT JOIN BPMS_PROMO_MAPPING bpm ON bsr.Srq_Id = bpm.Srq_Id LEFT JOIN BPMS_PROMOCODE bp ON bpm.Promocode_Id = bp.Promocode_Id LEFT JOIN BPMS_PROBLEM_CATEGORIES_TRANSLATIONS bpct ON bsr.Srq_Problem_Category = bpct.Problem_Id AND bpct.Language_Code =? LEFT JOIN BPMS_FEEDBACK bf ON bsr.Srq_Id = bf.Service_Id LEFT JOIN BPMS_EMPLOYEE be ON bsr.Adm_Id = be.Emp_Id LEFT JOIN BPMS_EMPLOYEE bet ON bsr.Technician_Id = bet.Emp_Id INNER JOIN BPMS_USER_APARTMENT_MAPPING buam ON bsr.Srq_Apartment_Id = buam.Apartment_Id AND bsr.User_Id = buam.User_Id")
	fmt.Println("admin id", input.Admin_Id)
	if input.User_Id != 0 || input.Srq_Id != 0 || input.Apartment_Id != 0 || input.Service_Id != "" || input.Admin_Id != 0 || input.Technician_Id != 0 || input.Status != " " || input.FromDate != " " || input.ToDate != " " || input.Problem != "" || input.Name != "" || input.Tech_Name != "" {
		srqQuery.WriteString(" WHERE ")
		countQuery.WriteString(" WHERE ")
		costQuery.WriteString(" WHERE ")

		var userId, srqId, apartmentId, serviceId, adminId, technicianId, problemId string

		if input.User_Id != 0 {
			userId = strconv.Itoa(input.User_Id)
		} else {
			userId = "NULL"
		}

		if input.Srq_Id != 0 {
			srqId = strconv.Itoa(input.Srq_Id)
		} else {
			srqId = "NULL"
		}

		if input.Apartment_Id != 0 {
			apartmentId = strconv.Itoa(input.Apartment_Id)
		} else {
			apartmentId = "NULL"
		}

		if input.Service_Id != "" {
			serviceId = input.Service_Id
		} else {
			serviceId = "NULL"
		}

		if input.Admin_Id != 0 {
			adminId = strconv.Itoa(input.Admin_Id)
		} else {
			adminId = "NULL"
		}

		if input.Technician_Id != 0 {
			technicianId = strconv.Itoa(input.Technician_Id)
		} else {
			technicianId = "NULL"
		}

		if input.Problem_Category != 0 {
			problemId = strconv.Itoa(input.Problem_Category)
		} else {
			problemId = "NULL"
		}

		srqQuery.WriteString("(" + srqId + " IS NULL OR bsr.Srq_Id = " + srqId + " ) AND  (" + userId + "  IS NULL OR bsr.User_Id = " + userId + " ) AND  (" + apartmentId + "  IS NULL OR bsr.Srq_Apartment_Id = " + apartmentId + " ) AND  (" + adminId + "  IS NULL OR bsr.Adm_Id = " + adminId + " ) AND  (" + technicianId + "  IS NULL OR bsr.Technician_Id = " + technicianId + " ) AND (" + problemId + "  IS NULL OR bsr.Srq_Problem_Category = " + problemId + " )")

		countQuery.WriteString("(" + srqId + " IS NULL OR bsr.Srq_Id = " + srqId + " ) AND  (" + userId + "  IS NULL OR bsr.User_Id = " + userId + " ) AND  (" + apartmentId + "  IS NULL OR bsr.Srq_Apartment_Id = " + apartmentId + " ) AND (" + adminId + "  IS NULL OR bsr.Adm_Id = " + adminId + " ) AND  (" + technicianId + "  IS NULL OR bsr.Technician_Id = " + technicianId + " ) AND (" + problemId + "  IS NULL OR bsr.Srq_Problem_Category = " + problemId + " )")

		costQuery.WriteString("(" + srqId + " IS NULL OR bsr.Srq_Id = " + srqId + " ) AND  (" + userId + "  IS NULL OR bsr.User_Id = " + userId + " ) AND  (" + apartmentId + "  IS NULL OR bsr.Srq_Apartment_Id = " + apartmentId + " ) AND (" + adminId + "  IS NULL OR bsr.Adm_Id = " + adminId + " ) AND  (" + technicianId + "  IS NULL OR bsr.Technician_Id = " + technicianId + " ) AND (" + problemId + "  IS NULL OR bsr.Srq_Problem_Category = " + problemId + " )")

		if input.Problem != "" {
			srqQuery.WriteString(" AND bpct.Problem_Name LIKE '%" + input.Problem + "%' ")
			countQuery.WriteString(" AND bpct.Problem_Name LIKE '%" + input.Problem + "%' ")
			costQuery.WriteString(" AND bpct.Problem_Name LIKE '%" + input.Problem + "%' ")

		}
		if input.FromDate != "" && input.ToDate != "" {
			fmt.Println("datewise")
			srqQuery.WriteString(" AND (DATE(bsr.Created_Date)) BETWEEN '" + input.FromDate + "' AND '" + input.ToDate + "'")
			countQuery.WriteString(" AND (DATE(bsr.Created_Date)) BETWEEN '" + input.FromDate + "' AND '" + input.ToDate + "'")
			costQuery.WriteString(" AND (DATE(bsr.Created_Date)) BETWEEN '" + input.FromDate + "' AND '" + input.ToDate + "'")
		}
		if input.Service_Id != "" {
			srqQuery.WriteString(" AND bsr.Srq_Service IN (" + serviceId + ")")
			countQuery.WriteString(" AND bsr.Srq_Service IN (" + serviceId + ")")
			costQuery.WriteString(" AND bsr.Srq_Service IN (" + serviceId + ")")
		}
		if input.Name != "" {
			srqQuery.WriteString(" AND bu.User_Name LIKE '%" + input.Name + "%' ")
			countQuery.WriteString(" AND bu.User_Name LIKE '%" + input.Name + "%' ")
			costQuery.WriteString(" AND bu.User_Name LIKE '%" + input.Name + "%' ")
		}

		if input.Payment_Status != "" {
			srqQuery.WriteString(" AND bsr.Srq_Payment_Status ='" + input.Payment_Status + "' ")
			countQuery.WriteString(" AND bsr.Srq_Payment_Status ='" + input.Payment_Status + "' ")
			costQuery.WriteString(" AND bsr.Srq_Payment_Status  ='" + input.Payment_Status + "' ")
		}

		if input.Tech_Name != "" {
			srqQuery.WriteString(" AND bet.Emp_Name LIKE '%" + input.Tech_Name + "%' ")
			countQuery.WriteString(" AND bet.Emp_Name LIKE '%" + input.Tech_Name + "%' ")
			costQuery.WriteString(" AND bet.Emp_Name LIKE '%" + input.Tech_Name + "%' ")
		}

		if input.Status != "" {
			if strings.ToLower(input.Status) == "new" {
				statusNew := "AND ( bsr.Srq_Stage_1 = 1 AND bsr.Srq_Stage_2 = 0 AND bsr.Srq_Stage_8 = 0 )"
				srqQuery.WriteString(statusNew)
				countQuery.WriteString(statusNew)
				costQuery.WriteString(statusNew)

			} else if strings.ToLower(input.Status) == "assigned" {
				statusAssigned := "AND ( bsr.Srq_Stage_2 = 1 AND bsr.Srq_Stage_3 = 0 AND bsr.Srq_Stage_8 = 0 )"
				srqQuery.WriteString(statusAssigned)
				countQuery.WriteString(statusAssigned)
				costQuery.WriteString(statusAssigned)

			} else if strings.ToLower(input.Status) == "in-progress" {
				statusInprogress := "AND ( bsr.Srq_Stage_3 = 1 AND bsr.Srq_Stage_4 = 0 AND bsr.Srq_Stage_5 = 0 AND bsr.Srq_Stage_6 = 0 AND bsr.Srq_Stage_7 = 0 AND bsr.Srq_Stage_8 = 0 )"
				srqQuery.WriteString(statusInprogress)
				countQuery.WriteString(statusInprogress)
				costQuery.WriteString(statusInprogress)
			} else if strings.ToLower(input.Status) == "fixed" {
				statusFixed := "AND ( bsr.Srq_Stage_4 = 1 AND bsr.Srq_Stage_5 = 0 AND bsr.Srq_Stage_6 = 0 AND bsr.9 Srq_Stage_7= 0 AND bsr.Srq_Stage_8 = 0 )"
				srqQuery.WriteString(statusFixed)
				countQuery.WriteString(statusFixed)
				costQuery.WriteString(statusFixed)

			} else if strings.ToLower(input.Status) == "further action" {
				statusFurther := "AND ( bsr.Srq_Stage_5 = 1 AND bsr.Srq_Stage_7 = 0 AND bsr.Srq_Stage_8 = 0 )"
				srqQuery.WriteString(statusFurther)
				countQuery.WriteString(statusFurther)
				costQuery.WriteString(statusFurther)

			} else if strings.ToLower(input.Status) == "under observation" {
				statusUnderobsrv := "AND ( bsr.Srq_Stage_6 = 1 AND bsr.Srq_Stage_7= 0 AND bsr.Srq_Stage_8 = 0 )"
				srqQuery.WriteString(statusUnderobsrv)
				countQuery.WriteString(statusUnderobsrv)
				costQuery.WriteString(statusUnderobsrv)

			} else if strings.ToLower(input.Status) == "closed" {
				statusClosed := "AND ( bsr.Srq_Stage_7 = 1 )"
				srqQuery.WriteString(statusClosed)
				countQuery.WriteString(statusClosed)
				costQuery.WriteString(statusClosed)

			} else if strings.ToLower(input.Status) == "cancel" {
				statusCancel := "AND ( bsr.Srq_Stage_8 = 1 )"
				srqQuery.WriteString(statusCancel)
				countQuery.WriteString(statusCancel)
				costQuery.WriteString(statusCancel)
			} else if strings.ToLower(input.Status) == "open" {
				statusOpen := "AND ( bsr.Srq_Stage_7 = 0  AND bsr.Srq_Stage_8 = 0 AND (bsr.Srq_Stage_2 = 1 or bsr.Srq_Stage_3 = 1 or bsr.Srq_Stage_4 = 1 or bsr.Srq_Stage_5 = 1 or bsr.Srq_Stage_6 = 1)  )"
				srqQuery.WriteString(statusOpen)
				countQuery.WriteString(statusOpen)
				costQuery.WriteString(statusOpen)
			} else if strings.ToLower(input.Status) == "fullfilled" {
				statusOpen := "AND ( bsr.Srq_Stage_7 = 1 OR bsr.Srq_Stage_8 = 1)"
				srqQuery.WriteString(statusOpen)
				countQuery.WriteString(statusOpen)
				costQuery.WriteString(statusOpen)
			} else {
				log.Println("ServiceRepository: ServiceRequestFetch: fetchingservicerequest :invalidstatus : Status:", input.Status)
				return nil, errors.New(dto.InvalidStatusFetchServiceRequest)
			}

		}
	}

	srqQuery.WriteString(" GROUP BY bsr.Srq_Id ORDER BY bsr.Created_Date DESC limit ? offset ? ")

	srq_details := entity.FetchServiceRequest{}

	err = serviceRepo.db.Select(&srq_details.ServiceRequests, srqQuery.String(), input.Language_Code, input.Language_Code, input.Language_Code, input.Language_Code, input.Language_Code, pageLimit, int((input.Page-1)*pageLimit))
	if err != nil {
		log.Println("ServiceRepository: ServiceRequestFetch: fetchingservicerequest: apartmentid: ", input.Apartment_Id, " Error: ", err.Error())
		return nil, err
	}
	// countQuery.WriteString(" GROUP BY bsr.Srq_Id")
	var count int
	errRow := serviceRepo.db.Get(&count, countQuery.String(), input.Language_Code, input.Language_Code, input.Language_Code, input.Language_Code, input.Language_Code)
	if errRow != nil {
		log.Println("ServiceRepository: ServiceRequestFetch: fetchingservicerequest:  apartmentid: ", input.Apartment_Id, " Error: ", errRow.Error())
		return nil, errRow
	}
	fmt.Println(srqQuery.String())

	fmt.Println(countQuery.String())
	srq_details.Total_SRQ = count
	srq_details.Total_Page = count / pageLimit
	if count%pageLimit != 0 {
		srq_details.Total_Page++
	}
	//cost
	costQuery.WriteString(" ) as sub ")

	var totalcost sql.NullFloat64
	errCost := serviceRepo.db.Get(&totalcost, costQuery.String(), input.Language_Code, input.Language_Code, input.Language_Code, input.Language_Code, input.Language_Code)
	if errCost != nil {
		log.Println("ServiceRepository: ServiceRequestFetch: totalsrqcost:  apartmentid: ", input.Apartment_Id, " Error: ", errCost.Error())
		return nil, errCost
	} else {
		// totalcostStr := fmt.Sprintf("%.2f", totalcost)
		srq_details.Total_Cost = &totalcost.Float64
	}

	return &srq_details, nil

}

func (srqRepo *ServiceRequestRepository) UpdateServiceRequest(tx *sqlx.Tx, User_Id int, Srq_Id int, Status string, Srq_Problem_Desc string, timeslot string, img_paths []string, language int, payment_mode string, payment_status string) (srqList []entity.ServiceRequest, err error) {

	var errv error
	if payment_mode != "" || payment_status != "" {
		query := "UPDATE `BPMS_SERVICE_REQUEST` SET Srq_Payment_Mode = IFNULL(?, Srq_Payment_Mode), Srq_Payment_Status = IFNULL(?, Srq_Payment_Status) WHERE User_Id = ? AND  Srq_Id = ?"
		_, errv = tx.Exec(query, NewNullString(payment_mode), NewNullString(payment_status), User_Id, Srq_Id)
		if errv != nil {
			tx.Rollback()
			log.Println("ServiceRequestRepository: UpdateServiceRequest: updatingpaymentinfo :Input: ", User_Id, Srq_Id, "Error ", errv.Error())
			return nil, errv
		}
	} else {

		var stage_3 int
		query := "SELECT Srq_Stage_3 FROM `BPMS_SERVICE_REQUEST` WHERE Srq_Id =? "
		errStatus := tx.Get(&stage_3, query, Srq_Id)
		if errStatus != nil {
			tx.Rollback()
			log.Println("ServiceRequestRepository: UpdateServiceRequest: gettingstatus : Srq_Id:", Srq_Id, " Error: ", errStatus.Error())
			return nil, errStatus
		}

		if stage_3 == 0 {
			//cancel
			if strings.ToLower(Status) == "cancelled" {
				query := "UPDATE `BPMS_SERVICE_REQUEST` SET Srq_Stage_8 = 1, Srq_Stage_8_ts = CURRENT_TIMESTAMP() WHERE User_Id = ? AND Srq_Id = ?"
				_, errv := tx.Exec(query, User_Id, Srq_Id)
				if errv != nil {
					tx.Rollback()
					log.Println("ServiceRequestRepository: UpdateServiceRequest: Input: ", User_Id, Srq_Id, "Error ", errv.Error())
					return nil, errv
				}

			}

			if len(img_paths) > 0 {
				fmt.Println(img_paths)
				del := "DELETE FROM BPMS_SRQ_IMAGE WHERE Srq_Id =?"
				_, errDel := tx.Exec(del, Srq_Id)
				if errDel != nil {
					tx.Rollback()
					log.Println("ServiceRequestRepository: UpdateServiceRequest: Input: srq_id", Srq_Id, "Error ", errDel.Error())
					return nil, errDel

				}
				for _, img_path := range img_paths {
					imgQuery := "INSERT INTO `BPMS_SRQ_IMAGE`(`Image_path`, `Srq_Id`) VALUES (?,?)"
					_, errImg := tx.Exec(imgQuery, img_path, Srq_Id)
					fmt.Println(img_path)
					if errImg != nil {
						tx.Rollback()
						log.Println("ServiceRepository: UpdateServiceRequest: updatingImage: input: ", Srq_Id, " Error: ", errImg.Error())
						return nil, errImg
					}

				}

			}
			if timeslot != "" {
				log.Println("updatedtimeslot", timeslot)
				query := "UPDATE `BPMS_SERVICE_REQUEST`  SET Srq_Visit_Timeslot = ? WHERE User_Id = ? AND  Srq_Id = ?"
				_, errv = tx.Exec(query, timeslot, User_Id, Srq_Id)
				if errv != nil {
					tx.Rollback()
					log.Println("ServiceRequestRepository: UpdateServiceRequest: updatetimeslot : Input: ", User_Id, Srq_Id, "Error ", errv.Error())
					return nil, errv
				}
			}
			query := "UPDATE `BPMS_SERVICE_REQUEST` SET Srq_Problem_Desc = IFNULL(?, Srq_Problem_Desc)  WHERE User_Id = ? AND  Srq_Id = ?"
			_, errv = tx.Exec(query, NewNullString(Srq_Problem_Desc), User_Id, Srq_Id)
			if errv != nil {
				tx.Rollback()
				log.Println("ServiceRequestRepository: UpdateServiceRequest: Input: ", User_Id, Srq_Id, "Error ", errv.Error())
				return nil, errv
			}

		} else {
			log.Println("ServiceRequestRepository: UpdateServiceRequest: cancelrequest : Srq_Id:", Srq_Id, ": userId:", User_Id)
			return nil, errors.New(dto.ErrorInvalidUpdateSrq)
		}
	}

	errs := tx.Select(&srqList, "SELECT bsr.Srq_Id, bsr.Srq_Key, bsr.Srq_Service, bst.Service_Name, bs.Service_Description, bs.Service_Image_Large, bs.Service_Image_Small, bsr.Request_Type, bsr.Parent_Request, bsr.Child_Request, bsr.Srq_Apartment_Id, bat.Apartment_Name,ba.Apartment_Image, bsat.State_Name, bct.City_Name, bsr.User_Id, bu.User_Name, bu.User_Mobno, bu.User_Image, buam.Block, buam.Door_No, bsr.Adm_Id, be.Emp_Name AS Admin_Name, be.Emp_Mobno AS Admin_Mobno, bsr.Admin_Comments, bsr.Technician_Id, bet.Emp_Name As Technician_Name, bet.Emp_Mobno AS Technician_Mobno, bet.Emp_Image AS Tech_Image, bsr.Technician_Comments, bpm.Promocode_Id, bp.Name, bp.Discount, bp.Max_Discount, bsr.Srq_Service_Cost,bsr.Srq_Material_Cost, bsr.Srq_Labour_Cost, bsr.Srq_Tax_Percentage, bsr.Srq_Tax, bsr.Srq_Discount_Amount, bsr.Srq_Other_Cost, bsr.Srq_Special_Discount, bsr.Srq_Final_Cost, bsr.Quote_Service_Cost,bsr.Quote_Labour_Cost, bsr.Quote_Material_Cost, bsr.Quote_Tax, bsr.Quote_Other_Cost, bsr.Srq_Quote_Cost, bsr.Srq_Stage_1, bsr.Srq_Stage_1_ts, bsr.Srq_Stage_2, bsr.Srq_Stage_2_ts, bsr.Srq_Stage_3, bsr.Srq_Stage_3_ts, bsr.Srq_Stage_4, bsr.Srq_Stage_4_ts, bsr.Srq_Stage_5, bsr.Srq_Stage_5_ts, bsr.Srq_Stage_6, bsr.Srq_Stage_6_ts, bsr.Srq_Stage_7, bsr.Srq_Stage_7_ts, bsr.Srq_Stage_8, bsr.Srq_Stage_8_ts, bsr.Srq_Stage_9, bsr.Srq_Stage_9_ts, bsr.Is_Happycode_Validated, bsr.Srq_Payment_Mode, bsr.Srq_Payment_Status, bsr.Srq_Quote_Status, bsr.Srq_Problem_Desc, bsr.Srq_Problem_Category,bpct.Problem_Name, bsr.Srq_Category_others,GROUP_CONCAT(bsi.Image_Path) As SRQ_Image, bsr.Srq_Visit_Timeslot, bsr.Srq_Instructions, bsr.Created_Date, bf.Feedback, bf.Rating FROM BPMS_SERVICE_REQUEST bsr INNER JOIN BPMS_SERVICE_TRANSLATIONS bst ON bsr.Srq_Service = bst.Service_Id And bst.Language_Code =? INNER JOIN BPMS_SERVICE bs ON bsr.Srq_Service = bs.Service_Id LEFT JOIN BPMS_APARTMENT ba ON bsr.Srq_Apartment_Id = ba.Apartment_Id INNER JOIN BPMS_APARTMENT_TRANSLATIONS bat ON bsr.Srq_Apartment_Id = bat.Apartment_Id AND bat.Language_Code =? INNER JOIN BPMS_STATE_TRANSLATIONS bsat ON ba.State = bsat.State_Id And bsat.Language_Code =? INNER JOIN BPMS_CITY bc ON ba.State = bc.State_Id INNER JOIN BPMS_CITY_TRANSLATIONS bct ON bc.City_Id = bct.City_Id AND bct.Language_Code =? INNER JOIN BPMS_USER bu ON bsr.User_Id = bu.User_Id LEFT JOIN BPMS_PROMO_MAPPING bpm ON bsr.Srq_Id = bpm.Srq_Id LEFT JOIN BPMS_PROMOCODE bp ON bpm.Promocode_Id = bp.Promocode_Id LEFT JOIN BPMS_PROBLEM_CATEGORIES_TRANSLATIONS bpct ON bsr.Srq_Problem_Category = bpct.Problem_Id AND bpct.Language_Code =? LEFT JOIN BPMS_FEEDBACK bf ON bsr.Srq_Id = bf.Service_Id LEFT JOIN BPMS_EMPLOYEE be ON bsr.Adm_Id = be.Emp_Id LEFT JOIN BPMS_EMPLOYEE bet ON bsr.Technician_Id = bet.Emp_Id LEFT JOIN BPMS_SRQ_IMAGE bsi ON bsr.Srq_Id = bsi.Srq_Id INNER JOIN BPMS_USER_APARTMENT_MAPPING buam ON bsr.Srq_Apartment_Id = buam.Apartment_Id AND bsr.User_Id = buam.User_Id WHERE bsr.Srq_Id=?", language, language, language, language, language, Srq_Id)
	if errs != nil {
		log.Println("ServiceRequestRepository: UpdateServiceRequest: Input: ", Srq_Id, "Error ", errs.Error())
		return nil, errs
	} else {
		log.Println("ServiceRequestRepository: UpdateServiceRequest: Input: ", Srq_Id)
		return srqList, nil
	}
}
