package com.gree.mall.manager.logic.admin;

import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.AdminCompanyWechatOther;
import com.gree.mall.manager.plus.service.AdminCompanyWechatOtherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class AdminCompanyWechatOtherLogic {

    @Autowired
    private AdminCompanyWechatOtherService adminCompanyWechatOtherService;
    @Autowired
    CommonLogic commonLogic;

    /**
     * 获取小程序配置信息
     */
    public AdminCompanyWechatOther getOne(HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);


        return adminCompanyWechatOtherService.lambdaQuery()
                .last("limit 1")
                .one();
    }

    /**
     * 初始化其他配置
     */
    public void init(String companyId, String companyName) {
        adminCompanyWechatOtherService.lambdaUpdate()
                .eq(AdminCompanyWechatOther::getCompanyWechatId, companyId)
                .remove();


        AdminCompanyWechatOther other = new AdminCompanyWechatOther();
        other.setCompanyWechatId(companyId);
        other.setCompanyName(companyName);
        other.insert();
    }

    /**
     * 更新其他配置
     */
    public void update(AdminCompanyWechatOther other,HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        adminCompanyWechatOtherService.lambdaUpdate()
                .eq(AdminCompanyWechatOther::getCompanyWechatId, "0")
                .remove();
        other.setCompanyWechatId("0");
        other.setCompanyName("平台");
        other.setCreateTime(new Date());
        other.insert();
    }


}
