package com.sifang.modules.sunshine.SystemSetting.dao;

import com.sifang.modules.sunshine.SystemSetting.entity.TraderPcd;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

@Repository
public class TraderPcdDao extends BaseDao<TraderPcd> {

    public List<TraderPcd> findListByTraderId(String traderId) {
        return find("from TraderPcd where traderId = :p1 order by createTime,sort", new Parameter(traderId));
    }

    /**
     * 校验地址是否授权
     */
    public boolean checkAuth(String traderId, String province, String city, String area) {
        List<TraderPcd> pcdList = find("from TraderPcd where traderId = :p1 and province = :p2", new Parameter(traderId, province));
        if (pcdList == null || pcdList.size() <= 0) {
            return false;
        }
        for (TraderPcd pcd : pcdList) {
            if (StringUtil.isBlank(pcd.getCity())) {
                return true;
            } else if (Objects.equals(pcd.getCity(), city)) {
                if (StringUtil.isBlank(pcd.getArea())
                        || Objects.equals(pcd.getArea(), area)) {
                    return true;
                }
            }
        }
        return false;
    }
}
