package com.yihu.wlyy.service.app.hospital;

import com.yihu.wlyy.entity.organization.Hospital;
import com.yihu.wlyy.entity.organization.HospitalDept;
import com.yihu.wlyy.repository.organization.HospitalDeptDao;
import com.yihu.wlyy.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016.09.20.
 */

@Component
@Transactional(rollbackFor = Exception.class)
public class HospitalDeptService extends BaseService {
    @Autowired
    private HospitalDeptDao hospitalDeptDao;
    @Autowired
    JdbcTemplate jdbcTemplate;

    public Page<HospitalDept> findDeptByHsoptail(String hospital, String key, long id, int pageSize) {
        if (pageSize <= 0) {
            pageSize = 10;
        }
        // 排序
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        // 分页信息
        Pageable pageRequest = new PageRequest(0, pageSize, sort);
        // 查询全部
        Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
        if (StringUtils.isNotEmpty(key)) {
            filters.put("name", new SearchFilter("name", SearchFilter.Operator.LIKE, key));
        }
        filters.put("hospital", new SearchFilter("hospital", SearchFilter.Operator.EQ,hospital));
        if (id > 0) {
            filters.put("id", new SearchFilter("id", SearchFilter.Operator.LT, id));
        }
        // 未作废
        filters.put("del", new SearchFilter("del", SearchFilter.Operator.EQ, "1"));
        Specification<HospitalDept> spec = DynamicSpecifications.bySearchFilter(filters.values(), HospitalDept.class);
        return hospitalDeptDao.findAll(spec,pageRequest);
    }

    /**
     * 查询科室列表
     *
     * @param hospital
     * @param key
     * @param page
     * @param pagesize
     * @return
     */
    public List<HospitalDept> getHospitalDept(String hospital, String key, int page, int pagesize){
        String sql = "select * from dm_hospital_dept " +
                " where" +
                "    exists (select * from wlyy_doctor where wlyy_doctor.dept = dm_hospital_dept.code)" +
                " and hospital = ? ";
        List<Object> args = new ArrayList<>();

        args.add(hospital);

        if(StringUtils.isNotEmpty(key)){
            sql += " and name like ? ";
            args.add("%" + key + "%");
        }

        sql += " limit "+ (page*pagesize) + "," + pagesize;

        List<HospitalDept> hosList = jdbcTemplate.query(sql,args.toArray(),new BeanPropertyRowMapper(HospitalDept.class));

        return hosList;
    }

}
