package cn.com.ty.lift.enterprise.region.dao.mapper;

import cn.com.ty.lift.common.constants.SqlConstants;
import cn.com.ty.lift.enterprise.region.dao.entity.Region;
import cn.com.ty.lift.enterprise.region.dao.entity.model.RegionBO;
import cn.com.ty.lift.enterprise.region.dao.entity.model.RegionResponse;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 区域表 Mapper 接口
 * </p>
 *
 * @author wang-hai-cheng
 * @since 2019-12-09
 */
public interface RegionMapper extends BaseMapper<Region> {
    @Select("select city_code cityCode, province_code provinceCode from region where id = #{id}")
    List<Map<String, Object>> selectProjectRegions(Long id);

    @Select("select region_id regionId,num from project where mt_company_id = #{mtCompanyId}")
    List<Map<String, Object>> selectProjects(Long mtCompanyId);

    @Select("select count(*) count from project where region_id= #{regionId}")
    Map<String, Object> selectProjectCount(Long regionId);

    @Select("select code,name from area_code")
    List<Map<String, Object>> selectAreaCodes();

    @Select("select user_id userId,name from user_info where user_id in (${userOrClerks})")
    List<Map<String, Object>> selectRegionMember(@Param("userOrClerks") String userOrClerks);

    @Select("select name,code from area_code where parent=#{cityCode}")
    List<Map<String, Object>> areas(String cityCode);

    @Select("select name from user_info where user_id = #{userId}")
    String selectNameByUserId(Long userId);

    @Select("select id,area_name areaName from region where mt_company_id=#{mtCompanyId} and area_name like #{areaName}")
    List<RegionBO> selectIdAndNameLikeName(Long mtCompanyId, String areaName);

    @Select("select project_id from project_user where user_id = #{userId} and mt_company_id = #{mtCompanyId} and user_role = 'REGION_DIRECTOR'")
    List<Long> selectProjectIdByRegionDirectorId(Long userId, Long mtCompanyId);

    @Update(SqlConstants.UPDATE_REGION_DIRECTOR)
    void updateRegionDirector(List<Long> projectIds, Long userId, Long mtCompanyId);

    @Select(SqlConstants.QUERY_REGIONS)
    IPage<RegionResponse> regions(IPage<RegionResponse> page, Long mtCompanyId, String name);
}