package com.skr.mdm.api.app.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.skr.mdm.api.app.service.AppTypeService;
import com.skr.mdm.bean.qo.app.AppTypeQO;
import com.skr.mdm.bean.vo.appstore.AppTypeVO;
import com.skr.mdm.dao.AppTypeDao;
import com.skr.mdm.dao.MdmAppStoreDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.AppTypeDO;
import com.skr.mdm.entity.MdmAppStoreDO;
import com.skr.mdm.enums.DeleteStateEnum;
import com.skr.mdm.enums.LogAdminTypeEnum;
import com.skr.mdm.enums.ResultCodeEnum;
import com.skr.mdm.handle.LogAdminHandler;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author nfq
 * @date 2022-11-21 14:13
 */
@Service
public class AppTypeServiceImpl implements AppTypeService {

    private final String MODULE = "应用管理";
    @Autowired
    private AppTypeDao appTypeDao;
    @Autowired
    private MdmAppStoreDao appStoreDao;
    @Autowired
    private LogAdminHandler logAdminHandler;

    @Override
    public ResponseResult listPage(Integer pageNum, Integer pageSize, LoginAdminDTO loginAdminDTO) {
        PageHelper.startPage(pageNum, pageSize);
        return ResponseResult.success(PageUtil.getPageResult(listAll(loginAdminDTO)));
    }

    @Override
    public List<AppTypeVO> listAll(LoginAdminDTO loginAdminDTO) {
        return appTypeDao.listAppTypeVO(loginAdminDTO.getCompanyId());
    }

    @Override
    public ResponseResult add(AppTypeQO appTypeQO, LoginAdminDTO loginAdminDTO) {
        Long count = new LambdaQueryChainWrapper<>(appTypeDao)
                .eq(AppTypeDO::getAppTypeName, appTypeQO.getAppTypeName())
                .eq(AppTypeDO::getCompanyId, loginAdminDTO.getCompanyId())
                .count();
        if (count > 0) {
            return ResponseResult.failure("应用类型已存在");
        }
        AppTypeDO appTypeDO = AppTypeDO.builder()
                .companyId(loginAdminDTO.getCompanyId())
                .appTypeName(appTypeQO.getAppTypeName())
                .build();
        appTypeDao.insert(appTypeDO);

        String content = "应用类型添加" + appTypeQO.getAppTypeName();
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.APP_TYPE.getLogType(), content, MODULE, null);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult update(AppTypeQO appTypeQO, LoginAdminDTO loginAdminDTO) {
        Long count = new LambdaQueryChainWrapper<>(appTypeDao)
                .eq(AppTypeDO::getAppTypeName, appTypeQO.getAppTypeName())
                .eq(AppTypeDO::getCompanyId, loginAdminDTO.getCompanyId())
                .ne(AppTypeDO::getId, appTypeQO.getId())
                .count();
        if (count > 0) {
            return ResponseResult.failure("应用类型已存在");
        }
        AppTypeDO beforeUpdateAppTypeDO = appTypeDao.selectById(appTypeQO.getId());

        AppTypeDO appTypeDO = AppTypeDO.builder()
                .id(appTypeQO.getId())
                .appTypeName(appTypeQO.getAppTypeName())
                .build();
        appTypeDao.updateById(appTypeDO);

        String content = "应用类型编辑 " + beforeUpdateAppTypeDO.getAppTypeName() + "修改为：" + appTypeDO.getAppTypeName();
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.APP_TYPE.getLogType(), content, MODULE, null);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult delete(List<Integer> appTypeIdList, LoginAdminDTO loginAdminDTO) {
        Long count = new LambdaQueryChainWrapper<>(appStoreDao)
                .eq(MdmAppStoreDO::getIsDelete, DeleteStateEnum.NON_DELETE.getState())
                .in(MdmAppStoreDO::getAppTypeId, appTypeIdList)
                .count();
        if (count > 0) {
            return ResponseResult.failure(ResultCodeEnum.APP_STORE_IN_USE);
        }
        appTypeDao.deleteBatchIds(appTypeIdList);
        List<String> appNameList = appTypeDao.selectBatchIds(appTypeIdList)
                .stream()
                .map(AppTypeDO::getAppTypeName)
                .collect(Collectors.toList());
        String content = "应用类型删除 " + StringUtils.join(appNameList, ",");
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.APP_TYPE.getLogType(), content, MODULE, null);
        return ResponseResult.success();
    }
}
