package com.iotechn.unimall.admin.api.shop.impl;

import java.util.*;

import com.iotechn.unimall.data.domain.AdminDO;
import com.iotechn.unimall.data.domain.RoleDO;
import com.iotechn.unimall.data.mapper.AdminMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.util.ExcelUtil;
import com.iotechn.unimall.data.mapper.shop.ShopAccountMapper;
import com.iotechn.unimall.data.domain.shop.ShopAccount;
import com.iotechn.unimall.admin.api.shop.IShopAccountService;
import com.iotechn.unimall.data.model.Page;

import org.springframework.transaction.annotation.Transactional;

/**
 * 电商账户表Service业务层处理
 *
 * @author jlb
 * @date 2022-05-28
 */
@Service
public class ShopAccountServiceImpl implements IShopAccountService {
    @Autowired
    private ShopAccountMapper shopAccountMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Boolean add(ShopAccount shopAccount, Long adminId) throws ServiceException {
        Date now = new Date();
        shopAccount.setGmtCreate(now);
        shopAccount.setGmtUpdate(now);
        shopAccount.setAdminId(adminId);
        return shopAccountMapper.insert(shopAccount) > 0;
    }

    @Override
    public Page<ShopAccount> list(String dangAccount, String dangPassword, String fuziAccount, String fuziPassword, String fuziCookie, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, Integer page, Integer limit) throws ServiceException {
        Wrapper<ShopAccount> wrapper = new EntityWrapper<ShopAccount>();
        if (!StringUtils.isEmpty(dangAccount)) {
            wrapper.eq("dang_account", dangAccount);
        }
        if (!StringUtils.isEmpty(dangPassword)) {
            wrapper.eq("dang_password", dangPassword);
        }
        if (!StringUtils.isEmpty(fuziAccount)) {
            wrapper.eq("fuzi_account", fuziAccount);
        }
        if (!StringUtils.isEmpty(fuziPassword)) {
            wrapper.eq("fuzi_password", fuziPassword);
        }
        if (!StringUtils.isEmpty(fuziCookie)) {
            wrapper.eq("fuzi_cookie", fuziCookie);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(userId)) {
            wrapper.eq("user_id", userId);
        }
        if (adminId != 1) {
            wrapper.eq("user_id", adminId);
        }
        wrapper.eq("delete_flag", 0);
        List<ShopAccount> list = shopAccountMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        for (ShopAccount shopAccount:list
             ) {
            if(shopAccount.getUserId() != null){
                AdminDO adminDO = adminMapper.selectById(shopAccount.getUserId());
                if(adminDO != null){
                    shopAccount.setUserName(adminDO.getUsername());
                }
            }
        }
        Integer count = shopAccountMapper.selectCount(wrapper);
        return new Page<ShopAccount>(list, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long id) {
        String[] ids = String.valueOf(id).split(",");
        for (String tt : ids) {
            ShopAccount tmp = shopAccountMapper.selectById(Long.parseLong(tt));
            if (tmp != null) {
                tmp.setDeleteFlag(1);
                shopAccountMapper.updateById(tmp);
            }
        }
        return true;
    }

    @Override
    public Boolean update(ShopAccount shopAccount, Long adminId) throws ServiceException {
        Date now = new Date();
        shopAccount.setGmtUpdate(now);
        shopAccount.setAdminId(adminId);
        return shopAccountMapper.updateById(shopAccount) > 0;
    }
    @Override
    public Boolean edit(ShopAccount shopAccount, Long adminId) throws ServiceException {
        Date now = new Date();
        shopAccount.setGmtUpdate(now);
        shopAccount.setAdminId(adminId);
        return shopAccountMapper.updateById(shopAccount) > 0;
    }
    @Override
    public ShopAccount get(Long id) throws ServiceException {
        return shopAccountMapper.selectById(id);
    }

    @Override
    public String export(String dangAccount, String dangPassword, String fuziAccount, String fuziPassword, String fuziCookie, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, Integer page, Integer limit) throws ServiceException {
        Wrapper<ShopAccount> wrapper = new EntityWrapper<ShopAccount>();
        if (!StringUtils.isEmpty(dangAccount)) {
            wrapper.eq("dang_account", dangAccount);
        }
        if (!StringUtils.isEmpty(dangPassword)) {
            wrapper.eq("dang_password", dangPassword);
        }
        if (!StringUtils.isEmpty(fuziAccount)) {
            wrapper.eq("fuzi_account", fuziAccount);
        }
        if (!StringUtils.isEmpty(fuziPassword)) {
            wrapper.eq("fuzi_password", fuziPassword);
        }
        if (!StringUtils.isEmpty(fuziCookie)) {
            wrapper.eq("fuzi_cookie", fuziCookie);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(userId)) {
            wrapper.eq("user_id", userId);
        }
        if (!StringUtils.isEmpty(adminId)) {
            wrapper.eq("admin_id", adminId);
        }
        List<ShopAccount> list = shopAccountMapper.selectList(wrapper);
        ExcelUtil<ShopAccount> util = new ExcelUtil<ShopAccount>(ShopAccount.class);
        return util.exportExcel(list, "操作日志");
    }
    @Override
    public List<Map<String, Object>> options(Long adminId) throws ServiceException {
        List<AdminDO> roleDOS = adminMapper.selectList(new EntityWrapper<>());
        List<Map<String,Object>> list = new LinkedList<>();
        roleDOS.forEach(item -> {
            Map<String,Object> map = new HashMap<>();
            map.put("value", item.getId());
            map.put("label", item.getUsername());
            list.add(map);
        });
        return list;
    }

}
