package com.sifang.modules.site.siteInfo.service;

import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.order.utils.Apiutils;
import com.sifang.modules.site.siteInfo.dao.SiteCompanyInfoDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmSite;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.common.utils.StringUtil;
import com.sifang.modules.sys.dao.UserDao;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class SiteCompanyInfoService extends BaseService {

    @Autowired
    private SiteCompanyInfoDao siteCompanyInfoDao;
    @Autowired
    private UserDao userDao;

    @Autowired
    private SessionDAO sessionDAO;

    /**
     * 获取服务商信息列表数据
     *
     * @param
     * @return
     */
    public Record getSiteCompanyData(User user) {
        Record rd = siteCompanyInfoDao.getSiteCompanyData(user);
        return rd;
    }

    /**
     * 修改资料保存
     *
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object saveBtn(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        try {
            String id = request.getParameter("idVal");//id
            String corporator = request.getParameter("corporator");//法人代表
            String contacts = request.getParameter("contacts");//联系人
            String mobile = request.getParameter("mobile");//联系电话
            String province = request.getParameter("province");//省
            String city = request.getParameter("city");//市
            String area = request.getParameter("area");//区
            String detailAddress = request.getParameter("detailAddress");//详细地址
            String imgs = request.getParameter("imgs");//营业执照
            String userId = request.getParameter("userId");
            User userNew = new User();
            if (StringUtil.isNotBlank(userId)) {
                userNew = userDao.getById(userId);
            } else {
                User user = UserUtils.getUser();
                userNew = userDao.getById(user.getId());
            }
            if (StringUtils.isNotBlank(id)) {
                CrmSite site = siteCompanyInfoDao.get(id);
                logger.info("获取对象信息CrmSite site:" + site + ",time:" + DateUtils.getDateTime());
                if (site != null) {
                    site.setCorporator(corporator);
                    site.setContacts(contacts);
                    site.setMobile(mobile);
                    site.setProvince(province);
                    site.setCity(city);
                    site.setArea(area);
                    site.setAddress(detailAddress);
                    site.setLicenseImg(imgs);
                    String datailsAddress = site.getProvince() + site.getCity() + site.getArea() + site.getAddress();
                    double[] dals = Apiutils.addressToGPS(datailsAddress);
                    if (dals != null) {
                        site.setLnglat(Double.toString(dals[0]) + "," + Double.toString(dals[1]));
                    }
                    siteCompanyInfoDao.save(site);
                    userNew.setMobile(mobile);
                    userDao.save(userNew);
                    logger.info("数据保存成功:" + site + ",time:" + DateUtils.getDateTime());
                    map.put("code", "200");
                } else {
                    map.put("code", "505");
                }
            } else {
                map.put("code", "500");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "205");
            logger.info("输出异常信息状态码:" + "205" + ",输出异常信息:" + e);
        }
        return map;
    }


    public Result uodateSitePasswork(String userId, String oldPasswork, String newPasswork) {
        User user = userDao.get(userId);
        String passMd5 = HashKit.md5(newPasswork);
        if (!HashKit.md5(oldPasswork).equals(user.getPassword())) {
            return Result.fail("202", "旧密码不正确!");
        }
        if (passMd5.equals(user.getPassword())) {
            return Result.fail("201", "新密码和旧密码不能一致!");
        }
        user.setPassword(passMd5);
        userDao.save(user);
        Session session = SecurityUtils.getSubject().getSession();
        sessionDAO.delete(session);
        SecurityUtils.getSubject().logout();
        return Result.ok();
    }


}
