package com.bootdo.portal.controller;

import com.bootdo.common.utils.EhcacheUtil;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.data.entity.TfInfoEnterprise;
import com.bootdo.data.service.TfInfoEnterpriseService;
import com.bootdo.datas.domain.DataDO;
import com.bootdo.datas.service.DataService;
import com.bootdo.portal.common.CommonConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 企业信息api控制层
 *
 * @author Mr.xiBel
 * @date 2022/5/10
 **/
@CrossOrigin
@RestController
@RequestMapping("/portal/enterprise")
public class EnterpriseInfoApiController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 服务对象
     */
    @Resource
    private TfInfoEnterpriseService tfInfoEnterpriseService;

    /**
     * 企业登陆
     *
     * @param parameter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/enterpriseLogin", method = RequestMethod.POST)
    public R login(@RequestBody Map<String, Object> parameter) {
        String companyName = (String) parameter.get("name");
        String password = (String) parameter.get("password");
        String uniformSocialCreditCode = (String) parameter.get("uniformSocialCreditCode");
        if (StringUtils.isBlank(companyName) || StringUtils.isBlank(password) || StringUtils.isBlank(uniformSocialCreditCode)) {
            return R.error("请检查参数");
        }

        // 获取企业信息
        TfInfoEnterprise infoEnterprise = tfInfoEnterpriseService.loginQuery(parameter);
        if (Objects.isNull(infoEnterprise)) {
            return R.error("当前企业不存在或已禁用");
        }

        // 登陆成功 放入缓存 返回token值
        String token = UUID.randomUUID().toString().replace("-", "");
        EhcacheUtil.getInstance().putTokenUser(token, infoEnterprise);
        return R.ok().put(CommonConstants.DATA_FIELD_DATA, token);
    }

    /**
     * 登出
     *
     * @param parameter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/enterpriseLoginOut", method = RequestMethod.POST)
    public R loginOut(@RequestBody Map<String, Object> parameter) {
        Object uid = parameter.get("uid");
        EhcacheUtil.getInstance().removeTokenUser((String) uid);
        return R.ok();
    }

    /**
     * 获取当前企业信息
     *
     * @param parameter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getEnterpriseInfo", method = RequestMethod.POST)
    public R getEnterpriseInfo(@RequestBody Map<String, Object> parameter) {
        // 检查用户是否登陆
        R checkEnterpriseInfoCache = checkEnterpriseInfoCache(parameter);
        if (!Objects.isNull(checkEnterpriseInfoCache)) {
            return checkEnterpriseInfoCache;
        }

        // 获取企业信息
        //TfInfoEnterprise infoEnterprise = tfInfoEnterpriseService.queryById((Integer) parameter.get("uid"));
        return R.ok().put(CommonConstants.DATA_FIELD_DATA, EhcacheUtil.getInstance()
                .getUserByToken((String) parameter.get("uid")));
    }


    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@RequestBody Map<String, Object> parameter) {
        // 检查用户是否登陆
        R checkEnterpriseInfoCache = checkEnterpriseInfoCache(parameter);
        if (!Objects.isNull(checkEnterpriseInfoCache)) {
            return checkEnterpriseInfoCache;
        }

        // 更新
        objectAssign(parameter);

        return R.ok();
    }

    /**
     * 更新企业信息
     *
     * @param parameter
     */
    private void objectAssign(Map<String, Object> parameter) {
        // 已登陆
        TfInfoEnterprise infoEnterpriseCache = EhcacheUtil.getInstance().getUserByToken((String) parameter.get("uid"));
        // 获取企业信息
        TfInfoEnterprise tfInfoEnterprise = tfInfoEnterpriseService.queryById(infoEnterpriseCache.getId());

        if (Objects.nonNull(tfInfoEnterprise)) {
            String uniformSocialCreditCode = (String) parameter.get("uniformSocialCreditCode");
            tfInfoEnterprise.setUniformSocialCreditCode(uniformSocialCreditCode);

            String companyName = (String) parameter.get("companyName");
            tfInfoEnterprise.setCompanyName(companyName);

            String industry = (String) parameter.get("industry");
            tfInfoEnterprise.setIndustry(industry);

            String contacts = (String) parameter.get("contacts");
            tfInfoEnterprise.setContacts(contacts);


            String mobile = (String) parameter.get("mobile");
            tfInfoEnterprise.setMobile(mobile);

            String address = (String) parameter.get("address");
            tfInfoEnterprise.setAddress(address);

            String email = (String) parameter.get("email");
            tfInfoEnterprise.setEmail(email);

            tfInfoEnterprise.setUpdateTime(new Date());

            // 入库并更新缓存
            tfInfoEnterpriseService.update(tfInfoEnterprise);
            EhcacheUtil.getInstance().putTokenUser((String) parameter.get("uid"), tfInfoEnterprise);
        }
    }

    /**
     * 检查用户是否能登陆
     *
     * @param parameter
     * @return
     */
    private R checkEnterpriseInfoCache(Map<String, Object> parameter) {
        Object uid = parameter.get("uid");
        if (Objects.isNull(uid)) {
            return R.error(499, "请先登陆系统");
        }

        // 这里需要企业登录后获取
        TfInfoEnterprise infoEnterprise = EhcacheUtil.getInstance().getUserByToken((String) uid);
        if (Objects.isNull(infoEnterprise)) {
            return R.error(499, "请先登陆系统");
        }

        return null;
    }
}
