package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.QyDjxx;
import com.ruoyi.system.domain.QyInfo;
import com.ruoyi.system.domain.QyPerson;
import com.ruoyi.system.mapper.QyDjxxMapper;
import com.ruoyi.system.mapper.QyInfoMapper;
import com.ruoyi.system.mapper.QyPersonMapper;
import com.ruoyi.system.mapper.YtjMapper;
import com.ruoyi.system.service.IQyInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * info表Service业务层处理
 *
 * @author ruoyi
 * @date 2019-09-10
 */
@Service
public class QyInfoServiceImpl implements IQyInfoService {
    @Autowired
    private QyInfoMapper qyInfoMapper;
    @Autowired
    private QyDjxxMapper qyDjxxMapper;
    @Autowired
    private QyPersonMapper qyPersonMapper;
    @Autowired
    private YtjMapper ytjMapper;

    /**
     * 查询info表
     *
     * @param id info表ID
     * @return info表
     */
    @Override
    public QyInfo selectQyInfoById(Long id) {
        return qyInfoMapper.selectQyInfoById(id);
    }

    /**
     * 查询info表列表
     *
     * @param qyInfo info表
     * @return info表
     */
    @Override
    public List<QyInfo> selectQyInfoList(QyInfo qyInfo) {
        return qyInfoMapper.selectQyInfoList(qyInfo);
    }

    /**
     * 新增info表
     *
     * @param qyInfo info表
     * @return 结果
     */
    @Override
    public int insertQyInfo(QyInfo qyInfo) {
        return qyInfoMapper.insertQyInfo(qyInfo);
    }

    /**
     * 修改info表
     *
     * @param qyInfo info表
     * @return 结果
     */
    @Override
    public int updateQyInfo(QyInfo qyInfo) {
        return qyInfoMapper.updateQyInfo(qyInfo);
    }

    /**
     * 删除info表对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteQyInfoByIds(String ids) {
        return qyInfoMapper.deleteQyInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除info表信息
     *
     * @param id info表ID
     * @return 结果
     */
    @Override
    public int deleteQyInfoById(Long id) {
        return qyInfoMapper.deleteQyInfoById(id);
    }


    /**
     * 企业信息保存
     *
     * @param
     * @return 结果
     */
    @Override
    public int submitFrom(HttpServletRequest request) {
        QyInfo info = new QyInfo();

        Date createtime=new Date();
        info.setCreatedate(createtime);
        String bsid = request.getParameter("bsid");// 唯一标识
        info.setBsid(bsid);
        String frname = request.getParameter("frname");// 唯一标识
        info.setFrname(frname);
        String laiyuan=request.getParameter("laiyuan");//来源
        info.setLaiyuan(laiyuan);
        String wtrcard= request.getParameter("wtrsfz");//委托人身份证
        info.setWtrcardno(wtrcard);
        String hzlx = request.getParameter("hzlx");//核准书类型
        String hzwh = request.getParameter("hzwh");//
        String nh = request.getParameter("nh");//
        String xh = request.getParameter("xh");//字号
        System.err.println(hzlx+","+hzwh+","+nh+","+xh);
        info.setQyhezhun(hzwh);
        String qyname = request.getParameter("qyname");//企业名称
        info.setQyname(qyname);
        String qylx = request.getParameter("qylx");//企业类型
        info.setQylx(qylx);
        String cxbj = request.getParameter("cxbj");//城乡标记
        info.setQyss(cxbj);
        String qyjyqx = request.getParameter("qyjyqx");//经营年限
        info.setQyjyqx(qyjyqx);

        if(!qyjyqx.equals("长期")){
            String qynx = request.getParameter("qynx");//年
            info.setQynx(Integer.valueOf(qynx));
        }
        String qyzczj = request.getParameter("qyzczj");//注册资金
        info.setQyzczj(qyzczj);
        String jycscq = request.getParameter("jycscq");//住所产权
        info.setJycscq(jycscq);
        String zsxz = request.getParameter("zsxz");//住所性质
        info.setJycsxz(zsxz);

        String lxdh = request.getParameter("lxdh");//联系电话
        info.setQylxdh(lxdh);
        String yzbm = request.getParameter("yzbm");//邮政编码
        info.setQyyoujifs(yzbm);
        String gyrs = request.getParameter("gyrs");//雇佣人数
        info.setQygyrs(gyrs);
        String isjc = request.getParameter("isjc");//是否军产
        info.setIsjc(isjc);
        String qycyrs = request.getParameter("qycyrs");//从业人数
        info.setQycyrs(qycyrs);

        String xzq = request.getParameter("xzq");//市场主体所在地
        String jycsz = request.getParameter("jycsz");//市区
        String jycsc = request.getParameter("jycsc");//村乡
        String xxdz = request.getParameter("xxdz");//详细地址
        info.setQyyoujiaddress(xzq+xxdz);
        info.setQyjycs("河北省"+ Global.getSuperCity()+xzq+xxdz);

        String HY1 = request.getParameter("HY1");//行业大类ID
        String Hy= ytjMapper.GetHyname(HY1);

        String result = request.getParameter("result");//
        String[] ll = result.split("-");//行业分类截取
        info.setHangye(Hy+"-"+result);
        String hy=ll[0];
        String hy1=ll[1];
        info.setHangye1(Hy);
        info.setHangye2(hy);
        info.setHangye3(hy1);
        String qyjyfw = request.getParameter("qyjyfw");//经营范围
        info.setQyjyfw(qyjyfw);

        List<QyPerson> person= qyPersonMapper.selectQyPersonByBsid(bsid);
        for (QyPerson p:person) {
            if (p.getRentype().equals("委托代理人")){
                info.setLqrqianming(p.getRenqianming());
            }
        }


        //创建djxx
        QyDjxx djxx=new QyDjxx();
        djxx.setBsid(bsid);
        String isdangjian = request.getParameter("isdangjian");//是否党建
        djxx.setIsdangjian(isdangjian);
        String dang = request.getParameter("dang");//党员标志
        djxx.setDyflg(dang);
        String djzz = request.getParameter("djzz");//党建组织

        String zzjz = request.getParameter("zzjz");//党组织建制
        djxx.setZzjz(zzjz);
        String zjfs = request.getParameter("zjfs");//党组件方式
        djxx.setZjfs(zjfs);
        String jldate = request.getParameter("jldate");//党组件时间
        djxx.setJldate(jldate);
        String biaozhi = request.getParameter("biaozhi");//组织标志
        djxx.setZzflg(biaozhi);
        String zzbz = request.getParameter("zzbz");//

        String dyrs = request.getParameter("dyrs");//党员预备人数
        djxx.setDyrs(dyrs);
        String shuji = request.getParameter("shuji");//书记标志
        djxx.setSjflg(shuji);
        String dybj = request.getParameter("dybj");//党员标志


        if(request.getParameter("IsHere").equals("1")){
            qyDjxxMapper.deleteQyDjxxByBsId(bsid);
            qyInfoMapper.deleteQyInfoByBsId(bsid);
        }
        int d=qyDjxxMapper.insertQyDjxx(djxx);
        int i=qyInfoMapper.insertQyInfo(info);

        if(d!=1||i!=1){
            return -1;
        }
        return 1;
    }
    @Override
    public QyInfo selectQyInfoByBsId(String bsid){
        return qyInfoMapper.selectQyInfoByBsId(bsid);
    }

    /**
     * 查询info表
     *
     * @param card sfz
     * @return info表
     */
    @Override
    public List<QyInfo> selectQyInfoByCard(String card){

        return qyInfoMapper.selectQyInfoByCard(card);
    }

    /**
     * 提交表单
     *
     * @param
     * @return 结果
     */
    @Override
    public int submitFrom(@Param("bsid") String bsid, @Param("flg")String flg, @Param("createdate") Date createTime){
        return  qyInfoMapper.submitFrom(bsid, flg, createTime);
    }
    /**
     * 法人&委托人信息保存导info中
     *
     * @param
     * @return 结果
     */
    @Override
    public int submitInfo(QyInfo info) {
        int i=qyInfoMapper.updateQyInfoBybsid(info);

        return  i;
    }
    /**
     * 根据时间查询企业信息
     * @param start
     * @param end
     */

    @Override
    public Map selectQyInfoByDate(String start, String end){
        return qyInfoMapper.selectQyInfoByDate(start,end);
    }

    @Override
    public  List<Map>  selectQyInfoByNumb(String  date){
        return  qyInfoMapper.selectQyInfoByNumb(date);
    }
    @Override
    public List<Map>  selectQyInfoToflg(){
        return qyInfoMapper.selectQyInfoToflg();
    }

    /**
     *
     * @param name  人名
     * @param phone  联系电话
     * @param gtname    企业名称
     * @param sendback  退回原因
     * @param flg   状态
     * @return
     */
    @Override
    public String sendMessage(String name, String phone, String gtname, String sendback, int flg){
        String info = null;
        //公司短信发送
        String url= "http://api.sms.cn/sms/";
        String parme="";
        try{
            if(flg==0){
                parme="ac=send&uid=qxspj&pwd=8d18d2eb316841db0998eeb67362ba4c&mobile="+phone
                        + "&content=" + name + "您好:您申请的《"+gtname+"》企业设立提交成功，请等待审核。【"+Global.getCityName()+"行政审批局】";
            }

            String result= HttpUtils.sendGet(url,parme);
            System.err.println(result);


        /*//矿区短信发送
        HttpClient httpclient = new HttpClient();
            PostMethod post = new PostMethod("http://api.ums86.com:8899/sms/Api/Send.do");//
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");
            post.addParameter("SpCode", "247007");
            post.addParameter("LoginName", "jxxzspj");
            post.addParameter("Password", "kqxzspj147");
        if(sendback==null) {
            post.addParameter("MessageContent", name + "您好:您申请的个体工商户《" + gtname + "》审核通过,请及时打印营业执照。");
            post.addParameter("UserNumber", phone);
            post.addParameter("SerialNumber", "");
            post.addParameter("ScheduleTime", "");
            post.addParameter("f", "1");
        }else{
            post.addParameter("MessageContent", name + "您好:您申请的个体工商户《" + gtname + "》审核未通过,原因："+sendback);
            post.addParameter("UserNumber", phone);
            post.addParameter("SerialNumber", "");
            post.addParameter("ScheduleTime", "");
            post.addParameter("f", "1");
        }
        httpclient.executeMethod(post);
        info = new String(post.getResponseBody(),"gbk");
        System.out.println(info);*/



        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }



}
