package com.dxhy.admin.sys.service.impl;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.admin.sys.config.AdminConfig;
import com.dxhy.admin.sys.dao.SellerUserDao;
import com.dxhy.admin.sys.dao.UserCompanyDao;
import com.dxhy.admin.sys.entity.SysDeptEntity;
import com.dxhy.admin.sys.entity.SysUserCompanyEntity;
import com.dxhy.admin.sys.entity.SysUserEntity;
import com.dxhy.admin.sys.service.SellerUserService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Constant;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.Query;

import lombok.extern.slf4j.Slf4j;

/**
 * 系统用户
 *
 * @author fth
 */
@Service("sellerUserService")
@Slf4j
public class SellerUserServiceImpl extends ServiceImpl<SellerUserDao, SysUserEntity> implements SellerUserService {

    @Resource
    private SellerUserDao sellerUserDao;
    @Resource
    private UserCompanyDao userCompanyDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String userName = (String)params.get("userName");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        IPage<SysUserEntity> page = this.page(new Query<SysUserEntity>().getPage(params),
            new QueryWrapper<SysUserEntity>()
                .eq(org.apache.commons.lang3.StringUtils.isNoneBlank(userName), "username", userName)
                .eq(org.apache.commons.lang3.StringUtils.isNoneBlank(AdminConfig.userCompany), "company",
                    AdminConfig.userCompany)
                .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER)));
        return new PageUtils(page);
    }

    @Override
    public JSONObject getUserTitle() {
        JSONObject json;
        String[] titlelist = {"登录名", "用户名"};
        String[] titleprop = {"loginname", "username"};
        // 需要设置返回列表标题时调用
        json = setTitleList(titlelist, titleprop);
        return json;
    }

    @Override
    public JSONObject getCompanyTitle() {
        JSONObject json;
        String[] titlelist = {"集团编码", "集团名称"};
        String[] titleprop = {"company", "taxname"};
        // 需要设置返回列表标题时调用
        json = setTitleList(titlelist, titleprop);
        return json;
    }

    public JSONObject setTitleList(String[] titlelist, String[] titleprop) {
        JSONObject titleList = new JSONObject();
        if (titlelist != null) {
            titleList.put("titlelist", titlelist);
        }

        if (titleprop != null) {
            titleList.put("titleprop", titleprop);
        }

        JSONObject returnJson = new JSONObject();
        returnJson.put("titlelist", titleList);
        return returnJson;
    }

    @Override
    public PageUtils queryNoBindCompany(Map<String, Object> params) {
        String userId = params.get("userId") + "";
        String company = params.get("company") + "";
        int currPage = Integer.parseInt(params.get("page") + "");
        int pageSize = Integer.parseInt(params.get("limit") + "");
        params.put("currIndex", (currPage - 1) * pageSize);
        params.put("pageSize", pageSize);
        DynamicContextHolder.push("manage" + DbConstant.BUSINESS_READ);
        int count = sellerUserDao.queryNoBindCompanyCount(userId, company, params);
        List<SysDeptEntity> list = sellerUserDao.queryNoBindCompany(userId, company, params);
        return new PageUtils(list, count, Integer.parseInt(params.get("limit") + ""),
            Integer.parseInt(params.get("page") + ""));
    }

    @Override
    public PageUtils queryBindCompany(Map<String, Object> params) {
        String userId = params.get("userId") + "";
        String company = params.get("company") + "";
        int currPage = Integer.parseInt(params.get("page") + "");
        int pageSize = Integer.parseInt(params.get("limit") + "");
        params.put("currIndex", (currPage - 1) * pageSize);
        params.put("pageSize", pageSize);
        DynamicContextHolder.push("manage" + DbConstant.BUSINESS_READ);
        int count = sellerUserDao.queryBindCompanyCount(userId, company, params);
        List<SysDeptEntity> list = sellerUserDao.queryBindCompany(userId, company, params);
        return new PageUtils(list, count, Integer.parseInt(params.get("limit") + ""),
            Integer.parseInt(params.get("page") + ""));
    }

    @Override
    public boolean bindCompany(Map<String, Object> params) {
        String companys = params.get("company") + "";
        String userId = params.get("userId") + "";
        DynamicContextHolder.push("manage" + DbConstant.BUSINESS_WRITE);
        String[] arr = companys.split(",");
        for (String company : arr) {
            SysUserCompanyEntity entity = new SysUserCompanyEntity();
            entity.setCompany(company);
            entity.setUserId(Long.parseLong(userId));
            userCompanyDao.insert(entity);
        }
        return true;
    }

    @Override
    public boolean cancelBindCompany(Map<String, Object> params) {
        String companys = params.get("company") + "";
        String userId = params.get("userId") + "";
        DynamicContextHolder.push("manage" + DbConstant.BUSINESS_WRITE);
        String[] arr = companys.split(",");
        if (arr.length > 0) {
            sellerUserDao.deleteCompany(arr, userId);
        }
        return true;
    }

}
