package ink.wgink.login.base.service.login.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.login.base.dao.login.ILoginFormDao;
import ink.wgink.login.base.pojo.dtos.login.LoginFormDTO;
import ink.wgink.login.base.pojo.vos.login.LoginFormVO;
import ink.wgink.login.base.service.login.ILoginFormService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: LoginFormServiceImpl
 * @Description: 登录form表单
 * @Author: wanggeng
 * @Date: 2021/7/29 3:45 下午
 * @Version: 1.0
 */
@Service
public class LoginFormServiceImpl extends DefaultBaseService implements ILoginFormService {

    @Autowired
    private ILoginFormDao loginFormDao;

    @Override
    public void save(LoginFormVO loginFormVO) {
        if (loginFormVO.getIsActive() == 1) {
            updateUnActiveAll();
        }
        String loginFormId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(loginFormVO);
        params.put("loginFormId", loginFormId);
        setSaveInfo(params);
        loginFormDao.save(params);
    }

    @Override
    public void remove(List<String> loginFormIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("loginFormIds", loginFormIds);
        setUpdateInfo(params);
        loginFormDao.remove(params);
    }

    @Override
    public void update(String loginFormId, LoginFormVO loginFormVO) {
        if (loginFormVO.getIsActive() == 1) {
            updateUnActiveAll();
        }
        Map<String, Object> params = HashMapUtil.beanToMap(loginFormVO);
        params.put("loginFormId", loginFormId);
        setUpdateInfo(params);
        loginFormDao.update(params);
    }

    @Override
    public void updateActive(String loginFormId) {
        updateUnActiveAll();
        Map<String, Object> params = getHashMap(4);
        // 激活当前
        params.put("isActive", 1);
        params.put("loginFormId", loginFormId);
        loginFormDao.updateActive(params);
    }

    @Override
    public void updateUnActive(String loginFormId) {
        Map<String, Object> params = getHashMap(4);
        params.put("isActive", 0);
        params.put("loginFormId", loginFormId);
        loginFormDao.updateActive(params);
    }

    /**
     * 取消全部激活
     */
    private void updateUnActiveAll() {
        Map<String, Object> params = getHashMap(4);
        params.put("isActive", 0);
        loginFormDao.updateActive(params);
    }

    @Override
    public List<LoginFormDTO> list(Map<String, Object> params) {
        return loginFormDao.list(params);
    }

    @Override
    public SuccessResultList<List<LoginFormDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<LoginFormDTO> loginFormDTOs = list(page.getParams());
        PageInfo<LoginFormDTO> pageInfo = new PageInfo<>(loginFormDTOs);
        return new SuccessResultList<>(loginFormDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public LoginFormDTO get(Map<String, Object> params) {
        return loginFormDao.get(params);
    }

    @Override
    public LoginFormDTO get(String loginFormId) {
        Map<String, Object> params = getHashMap(2);
        params.put("loginFormId", loginFormId);
        return get(params);
    }

    @Override
    public LoginFormDTO getActive() {
        Map<String, Object> params = getHashMap(2);
        params.put("isActive", 1);
        return get(params);
    }


}
