package com.lightinit.hsdatashow.impl.ms;

import com.lightinit.hsdatashow.common.UploadFileUtils;
import com.lightinit.hsdatashow.entity.ObjCategory;
import com.lightinit.hsdatashow.entity.ObjCategoryExample;
import com.lightinit.hsdatashow.entity.Organization;
import com.lightinit.hsdatashow.entity.OrganizationExample;
import com.lightinit.hsdatashow.mapper.ObjCategoryMapper;
import com.lightinit.hsdatashow.mapper.OrganizationMapper;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.ResultStateCode;
import com.lightinit.hsdatashow.model.ms.*;
import com.lightinit.hsdatashow.service.ms.IMsEducationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MsEducationServiceImpl implements IMsEducationService {

    @Autowired
    private ObjCategoryMapper objCategoryMapper ;

    @Autowired
    private OrganizationMapper organizationMapper ;


    @Override
    public List<EducationOrganizationResoultModel> queryList(EducationOrganizationSearchModel searchModel) {
        if (!StringUtils.isEmpty(searchModel.getOrgCode())){
            List<EducationOrganizationResoultModel> educationOrganizationResoultModels = organizationMapper.getEducationList(searchModel) ;
            return educationOrganizationResoultModels ;
        }else {
            return null;
        }
    }

    @Override
    public Integer queryListCount(EducationOrganizationSearchModel searchModel) {
        if (!StringUtils.isEmpty(searchModel.getOrgCode())){
            int count = organizationMapper.getEducationCount(searchModel) ;
            return count ;
        }else {
            return 0;
        }
    }

        public  String getListObj(String categoryName){
        ObjCategoryExample example = new ObjCategoryExample() ;
        ObjCategoryExample.Criteria criteria = example.createCriteria() ;
        criteria.andCategoryNameEqualTo(categoryName) ;
        List<ObjCategory> objCategories = objCategoryMapper.selectByExample(example) ;
            if (objCategories.size()>0){
                String orgCode = objCategories.get(0).getCategoryCode() ;
                return orgCode ;
            }else {
                return null;
            }
    }

    @Override
    @Transactional
    public ResultState<Long> add(EducationOrganizationAddModel educationOrganizationAddModel) {
        ResultState<Long> resultState = new ResultState<>() ;
        String orgCode = organizationMapper.getOrgCode(educationOrganizationAddModel.getParentCode()) ;
        Organization organization = new Organization() ;
        BeanUtils.copyProperties(educationOrganizationAddModel,organization);
        organization.setOrgCode(orgCode);
        int sqlResult = organizationMapper.insert(organization) ;
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(organization.getId());
        return resultState ;
    }

    @Override
    public EducationEditResultModel getEducationById(long id) {
        EducationEditResultModel educationEditResultModel = organizationMapper.getEducationById(id) ;
        if (educationEditResultModel!=null){
            String imgName = org.apache.commons.lang.StringUtils.isNotEmpty(educationEditResultModel.getPhoto()) ? UploadFileUtils.getFileNameByPath(educationEditResultModel.getPhoto()) : null ;
            educationEditResultModel.setImgName(imgName);
        }
        return educationEditResultModel ;
    }

    @Override
    public ResultState<Long> edit(EducationOrganizationEditModel educationOrganizationEditModel) {
        ResultState<Long> resultState = new ResultState<>() ;
        Organization organization = new Organization() ;
        BeanUtils.copyProperties(educationOrganizationEditModel,organization);
        OrganizationExample example = new OrganizationExample() ;
        OrganizationExample.Criteria criteria = example.createCriteria() ;
        criteria.andIdEqualTo(educationOrganizationEditModel.getId()) ;
        int sqlResult = organizationMapper.updateByExampleSelective(organization,example) ;
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(organization.getId());
        return resultState ;
    }

    @Override
    @Transactional
    public ResultState delete(long id) {
        ResultState<Long> resultState = new ResultState<>() ;
        int result = organizationMapper.deleteByPrimaryKey(id) ;
        if (!(result > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据删除失败");
        }
        resultState.setData((long) result);
        return resultState;
    }
}
