package com.zhongdao.jlr.business.dao.impl;

import com.dubh.common.dao.hibernate.BaseDaoHibernate;
import com.util.UtilTools;
import com.util.Vo;
import com.zhongdao.jlr.business.dao.SupplierDao;
import com.zhongdao.jlr.business.vo.Contract;
import com.zhongdao.jlr.business.vo.Destination;
import com.zhongdao.jlr.business.vo.Enterprise;
import com.zhongdao.jlr.business.vo.PageInfo;
import com.zhongdao.jlr.pojo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Repository
public class SupplierDaoImpl extends BaseDaoHibernate implements SupplierDao{

    @Override
    public Vo getContractInfo(HttpServletRequest request,String name,String id) {
        Vo vo=new Vo();
        //声明一个分页对象
        PageInfo pageInfo=new PageInfo();
        //获取页数和一页显示多少行并将其放入对象中
        pageInfo.setPage(UtilTools.getPage(request));
        pageInfo.setRows(UtilTools.getRows(request));
        //sql动态拼接
        StringBuffer sql=new StringBuffer("select c.id,c.contarct_code,c.contract_name,c.remark,c.app_code,c.create_time from contract c " +
                "where c.id in (7049,7079,7050,7080) and c.level = 2 and c.delete_flag != 1 " +
                "and c.id not in (select contract_id from jlr_contract_visual where jlr_user_id = "+id+")");
        //开始时间
        if (StringUtils.isNotBlank(name)) {
            sql.append("  and c.contract_name like '%" + name + "%'");
        }
        String countSql=sql.toString();
        String info=sql.append(" limit "+pageInfo.getCurrentIndex()+","+pageInfo.getRows()).toString();
        try {
            //获取用户信息总数
            List lists=getRsBySql(countSql);
            //分页获取用户信息
            List list = getRsBySql(info);
            if (list != null && list.size() > 0) {
                //将用户信息放入vo对象中
                vo.setRows(UtilTools.listTojsonArray(list, new String[]{"ID","CODE","NAME","REMARK","APPCODE","CREATETIME"}));
            }
            if(lists != null && lists.size() > 0) {
                vo.setTotal(lists.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    @Override
    public List<Contract> getAssignContract(String id) {
        List<Contract> lists=new ArrayList<>();
        String sql="select c.id,c.contarct_code,c.contract_name,c.remark,c.app_code,c.create_time from contract c " +
                "left join jlr_contract_visual j " +
                "on j.contract_id = c. id " +
                "where j.jlr_user_id="+id;
        try {
            //获取指定合同信息
            List list = getRsBySql(sql);
            if (list != null && list.size() > 0) {
                for(int i=0;i<list.size();i++){
                    Contract contract = new Contract();
                    Object[] objects = (Object[]) list.get(i);
                    contract.setId(Integer.valueOf(objects[0].toString()));
                    contract.setName(objects[2] == null ? "" : objects[2].toString());
                    //将对象放入集合中
                    lists.add(contract);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lists;
    }

    @Override
    public List<Destination> getAssignDestination(String id) {
        List<Destination> lists=new ArrayList<>();
        String sql="select c.id,c.name from contract_destination c " +
                "left join jlr_destination_visual j " +
                "on j.contract_destination_id = c.id " +
                "where j.jlr_user_id="+id;
        try {
            //获取指定目的地信息
            List list = getRsBySql(sql);
            if (list != null && list.size() > 0) {
                for(int i=0;i<list.size();i++){
                    Destination destination = new Destination();
                    Object[] objects = (Object[]) list.get(i);
                    destination.setId(Integer.valueOf(objects[0].toString()));
                    destination.setName(objects[1] == null ? "" : objects[1].toString());
                    //将对象放入集合中
                    lists.add(destination);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lists;
    }

    @Override
    public List<Enterprise> getAssignEnterprise(String id) {
        List<Enterprise> lists=new ArrayList<>();
        String sql="select s.id,s.name from supplier s " +
                "left join jlr_supplier_visual j " +
                "on s.id=j.supplier_id " +
                "where j.jlr_user_id="+id;
        try {
            //获取指定目的地信息
            List list = getRsBySql(sql);
            if (list != null && list.size() > 0) {
                for(int i=0;i<list.size();i++){
                    Enterprise enterprise = new Enterprise();
                    Object[] objects = (Object[]) list.get(i);
                    enterprise.setId(Integer.valueOf(objects[0].toString()));
                    enterprise.setName(objects[1] == null ? "" : objects[1].toString());
                    //将对象放入集合中
                    lists.add(enterprise);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lists;
    }

    @Override
    public Vo getEnterpriseInfo(HttpServletRequest request,String name,String id) {
        Vo vo=new Vo();
        //声明一个分页对象
        PageInfo pageInfo=new PageInfo();
        //获取页数和一页显示多少行并将其放入对象中
        pageInfo.setPage(UtilTools.getPage(request));
        pageInfo.setRows(UtilTools.getRows(request));
        //sql动态拼接
        StringBuffer sql=new StringBuffer("select s.id,s.name,s.code,s.phone_login_name,s.create_time from supplier s " +
                "where s.delete_flag != 1 " +
                "and s.id not in (select supplier_id from jlr_supplier_visual where jlr_user_id = "+id+")");
        //开始时间
        if (StringUtils.isNotBlank(name)) {
            sql.append("  and s.name like '%" + name + "%'");
        }
        String countSql=sql.toString();
        String info=sql.append(" limit "+pageInfo.getCurrentIndex()+","+pageInfo.getRows()).toString();
        try {
            //获取用户信息总数
            List lists=getRsBySql(countSql);
            //分页获取用户信息
            List list = getRsBySql(info);
            if (list != null && list.size() > 0) {
                //将用户信息放入vo对象中
                vo.setRows(UtilTools.listTojsonArray(list, new String[]{"ID","NAME","CODE","LOGINNAME","CREATETIME"}));
            }
            if(lists != null && lists.size() > 0) {
                vo.setTotal(lists.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    @Override
    public Vo getDestination(HttpServletRequest request, String name,String id) {
        Vo vo=new Vo();
        //声明一个分页对象
        PageInfo pageInfo=new PageInfo();
        //获取页数和一页显示多少行并将其放入对象中
        pageInfo.setPage(UtilTools.getPage(request));
        pageInfo.setRows(UtilTools.getRows(request));
        //sql动态拼接
        StringBuffer sql=new StringBuffer("select c.id,c.name,c.address,c.brand_name,c.create_time from contract_destination c " +
                "left join jlr_contract_visual j " +
                "on j.contract_id = c.contract_id " +
                "where c.delete_flag != 1 " +
                "and c.id not in (select contract_destination_id from jlr_destination_visual where jlr_user_id = "+id+") and j.jlr_user_id ="+id);
        //开始时间
        if (StringUtils.isNotBlank(name)) {
            sql.append("  and c.name like '%" + name + "%'");
        }
        String countSql=sql.toString();
        String info=sql.append(" limit "+pageInfo.getCurrentIndex()+","+pageInfo.getRows()).toString();
        try {
            //分页获取合同下的目的地信息总数
            List lists=getRsBySql(countSql);
            //分页获取合同下的目的地信息
            List list = getRsBySql(info);
            if (list != null && list.size() > 0) {
                //将用户信息放入vo对象中
                vo.setRows(UtilTools.listTojsonArray(list, new String[]{"ID","NAME","ADDRESS","BRAND","CREATETIME"}));
            }
            if(lists != null && lists.size() > 0) {
                vo.setTotal(lists.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    @Override
    public boolean insertContractInfo(JLRUserContract jlrUserContract) {
        try {
            //为用户指定合同
            saveOrUpdate(jlrUserContract);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean insertDestinationInfo(JLRDestination jlrDestination) {
        try {
            //指定目的地
            saveOrUpdate(jlrDestination);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean insertEnterpriseInfo(JLRSupplier jlrSupplier) {
        try {
            //指定服务商
            saveOrUpdate(jlrSupplier);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteDestinationInfo(int id,int uid) {
        try {
            //删除已指定的目的地信息
            deleteHql("delete JLRDestination where jlr_user_id = "+uid+" and contract_destination_id = "+id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteEnterpriseInfo(int sid,int uid) {
        try {
            //删除已指定的供应商信息
            deleteHql("delete JLRSupplier where jlr_user_id = "+uid+" and supplier_id = "+sid);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteContractInfo(int cid,int uid) {
        try {
            //删除已指定的合同信息
            deleteHql("delete JLRUserContract where jlr_user_id = "+uid+" and contract_id = "+cid);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public List<JLRUserContract> getContract(String id) {
        List<JLRUserContract> jlrUserContract=null;
        try {
            jlrUserContract=(List<JLRUserContract>) getRsByHql("from JLRUserContract where jlr_user_id = "+id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return jlrUserContract;
    }
}
