package com.usky.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.system.domain.MceSetting;
import com.usky.system.mapper.MceSettingMapper;
import com.usky.system.service.MceSettingService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 消息设置表 服务实现类
 * </p>
 *
 * @author han
 * @since 2024-05-09
 */
@Service
public class MceSettingServiceImpl extends AbstractCrudService<MceSettingMapper, MceSetting> implements MceSettingService {
    @Override
    public void add(MceSetting mceSetting){
        if (mceSetting.getId().equals(0)){
            if(StringUtils.isBlank(mceSetting.getAppMode())){
                mceSetting.setAppMode("{\"1\":true,\"2\":true,\"3\":true,\"4\":true,\"5\":true}");
            }
            if(StringUtils.isBlank(mceSetting.getPcMode())){
                mceSetting.setPcMode("{\"1\":true,\"2\":true,\"3\":true,\"4\":true,\"5\":true}");
            }
            if(StringUtils.isBlank(mceSetting.getWcMode())){
                mceSetting.setWcMode("{\"1\":true,\"2\":true,\"3\":true,\"4\":true,\"5\":true}");
            }

            mceSetting.setCreateBy(SecurityUtils.getUsername());
            mceSetting.setCreateTime(LocalDateTime.now());
            mceSetting.setTenantId(SecurityUtils.getTenantId());
            long longValue = SecurityUtils.getLoginUser().getSysUser().getDeptId();
            Integer intValue = (int) longValue;
            mceSetting.setDeptId(intValue);
            this.save(mceSetting);
        }else {
            mceSetting.setUpdateBy(SecurityUtils.getUsername());
            mceSetting.setUpdateTime(LocalDateTime.now());
            this.updateById(mceSetting);
        }
    }
    @Override
    public Map<String, Object> mceSettingInfo() {
        LambdaQueryWrapper<MceSetting> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(MceSetting::getCreateBy, SecurityUtils.getUsername())
                .eq(MceSetting::getTenantId,SecurityUtils.getTenantId());
        List<MceSetting> list1 = this.list(lambdaQuery);
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isEmpty(list1)) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("1", true);
            map1.put("2", true);
            map1.put("3", true);
            map1.put("4", true);
            map1.put("5", true);
            map.put("appMode",map1);
            map.put("pcMode",map1);
            map.put("wcMode",map1);
            map.put("id",0);
        }else {
            map.put("appMode", JSONObject.parseObject(list1.get(0).getAppMode()));
            map.put("pcMode",JSONObject.parseObject(list1.get(0).getPcMode()));
            map.put("wcMode",JSONObject.parseObject(list1.get(0).getWcMode()));
            map.put("id",list1.get(0).getId());
        }
        return map;
    }

    @Override
    public CommonPage<Map<String, Object>> mceSettingList(String createBy, Integer current, Integer size) {
        IPage<MceSetting> page = new Page<>(current, size);
        List<Map<String, Object>> list = new ArrayList<>();
        LambdaQueryWrapper<MceSetting> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(MceSetting::getTenantId,SecurityUtils.getTenantId())
                .like(StringUtils.isNotBlank(createBy),MceSetting::getCreateBy,createBy);
        page = this.page(page,lambdaQuery);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            for (int i = 0; i < page.getRecords().size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id",page.getRecords().get(i).getId());
                map.put("createBy",page.getRecords().get(i).getCreateBy());
                map.put("appMode",JSONObject.parseObject(page.getRecords().get(i).getAppMode()));
                map.put("pcMode",JSONObject.parseObject(page.getRecords().get(i).getPcMode()));
                map.put("wcMode",JSONObject.parseObject(page.getRecords().get(i).getWcMode()));
                list.add(map);
            }
        }
        return new CommonPage<>(list,page.getTotal(),page.getCurrent(),page.getSize());
    }
}
