package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.SysUserPagesetDTO;
import com.diagbot.entity.SysUserPageset;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.SysUserPagesetServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.SysUserPagesetQueryVO;
import com.diagbot.vo.SysUserPagesetSaveVO;
import com.diagbot.vo.SysUserPagesetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @author: gaodm
 * @time: 2020/6/12 10:25
 */
@Component
public class SysUserPagesetFacade extends SysUserPagesetServiceImpl {
    @Autowired
    @Qualifier("sysUserPagesetServiceImpl")
    private SysUserPagesetServiceImpl sysUserPagesetService;

    /**
     * 获取用户对应页面设置
     *
     * @param sysUserPagesetQueryVO 获取用户对应页面设置参数
     * @return 对应页面设置列表
     */
    public List<SysUserPagesetDTO> getPageSet(SysUserPagesetQueryVO sysUserPagesetQueryVO) {
        List<SysUserPageset> sysUserPagesetList = sysUserPagesetService.list(
                new QueryWrapper<SysUserPageset>()
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .eq("user_id", SysUserUtils.getCurrentPrincipleID())
                        .eq("page_type", sysUserPagesetQueryVO.getPageType())
                        .orderByAsc("order_no")
        );

        List<SysUserPagesetDTO> sysUserPagesetDTOList = new ArrayList<>();
        if (ListUtil.isEmpty(sysUserPagesetList)) {
            List<SysUserPageset> sysUserPagesetListDefault = sysUserPagesetService.list(
                    new QueryWrapper<SysUserPageset>()
                            .eq("is_deleted", IsDeleteEnum.N.getKey())
                            .eq("user_id", -1)
                            .eq("page_type", sysUserPagesetQueryVO.getPageType())
                            .orderByAsc("order_no")
            );
            sysUserPagesetDTOList = BeanUtil.listCopyTo(sysUserPagesetListDefault, SysUserPagesetDTO.class);
        } else {
            sysUserPagesetDTOList = BeanUtil.listCopyTo(sysUserPagesetList, SysUserPagesetDTO.class);
        }
        return sysUserPagesetDTOList;
    }

    /**
     * 保存用户对应页面设置
     *
     * @param sysUserPagesetSaveVO 保存用户对应页面设置参数
     * @return 是否成功
     */
    public Boolean savePageSet(SysUserPagesetSaveVO sysUserPagesetSaveVO) {
        if (ListUtil.isEmpty(sysUserPagesetSaveVO.getSysUserPagesetVOList())) {
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "列名输入列表不能为空");
        }
        sysUserPagesetService.remove(
                new QueryWrapper<SysUserPageset>()
                        .eq("user_id", SysUserUtils.getCurrentPrincipleID())
                        .eq("page_type", sysUserPagesetSaveVO.getPageType())
        );
        List<SysUserPageset> saveList = new ArrayList<>();
        Date now = DateUtil.now();
        String userId = SysUserUtils.getCurrentPrincipleID();
        for (SysUserPagesetVO sysUserPagesetVO : sysUserPagesetSaveVO.getSysUserPagesetVOList()) {
            SysUserPageset sysUserPageset = new SysUserPageset();
            BeanUtil.copyProperties(sysUserPagesetVO, sysUserPageset);
            sysUserPageset.setPageType(sysUserPagesetSaveVO.getPageType());
            sysUserPageset.setUserId(Long.valueOf(userId));
            sysUserPageset.setCreator(userId);
            sysUserPageset.setModifier(userId);
            sysUserPageset.setGmtCreate(now);
            sysUserPageset.setGmtModified(now);
            saveList.add(sysUserPageset);
        }
        return sysUserPagesetService.saveBatch(saveList);
    }

    /**
     * 获取默认列设置
     *
     * @param sysUserPagesetQueryVO
     * @return
     */
    public List<SysUserPagesetDTO> getDefaultPageSet(SysUserPagesetQueryVO sysUserPagesetQueryVO) {
        List<SysUserPagesetDTO> sysUserPagesetDTOList = new ArrayList<>();
        List<SysUserPageset> sysUserPagesetListDefault = sysUserPagesetService.list(
                new QueryWrapper<SysUserPageset>()
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .eq("user_id", -1)
                        .eq("page_type", sysUserPagesetQueryVO.getPageType())
                        .orderByAsc("order_no")
        );
        sysUserPagesetDTOList = BeanUtil.listCopyTo(sysUserPagesetListDefault, SysUserPagesetDTO.class);
        return sysUserPagesetDTOList;
    }
}