package com.fs.company.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.SecurityUtils;
import com.fs.company.domain.*;
import com.fs.company.mapper.*;
import com.fs.company.param.CompanyParam;
import com.fs.company.vo.CompanyOptionRespVO;
import com.fs.company.vo.CompanyVO;

import com.fs.system.domain.SysConfig;
import com.fs.system.mapper.SysConfigMapper;
import com.fs.system.service.ISysConfigService;
import com.google.gson.Gson;
import io.swagger.models.auth.In;
import lombok.Synchronized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.fs.company.service.ICompanyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 企业Service业务层处理
 *
 * @author fs
 * @date 2021-10-04
 */
@Service
public class CompanyServiceImpl implements ICompanyService
{
    Logger logger= LoggerFactory.getLogger(getClass());;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private CompanyDeptMapper deptMapper;

    @Autowired
    private CompanyRoleMapper roleMapper;
    @Autowired
    private CompanyPostMapper postMapper;
    @Autowired
    private CompanyUserMapper userMapper;
    @Autowired
    private CompanyUserRoleMapper userRoleMapper;
    @Autowired
    private CompanyUserPostMapper userPostMapper;
    @Autowired
    private ISysConfigService configService;


    @Autowired
    private SysConfigMapper sysConfigMapper;


    /**
     * 查询企业
     *
     * @param companyId 企业ID
     * @return 企业
     */
    @Override
    public Company selectCompanyById(Long companyId)
    {
        return companyMapper.selectCompanyById(companyId);
    }

    /**
     * 查询企业列表
     *
     * @param company 企业
     * @return 企业
     */
    @Override
    public List<Company> selectCompanyList(Company company)
    {
        return companyMapper.selectCompanyList(company);
    }

    /**
     * 新增企业
     *
     * @param company 企业
     * @return 结果
     */
    @Override
    @Transactional
    public R insertCompany(Company company)
    {
        //帐号是否存在
        if(userMapper.checkUserName(company.getUserName())>0){
            return R.error("管理员帐号已存在");
        }
        company.setCreateTime(DateUtils.getNowDate());
        if(companyMapper.insertCompany(company)>0){
            //写入帐号信息
            //创建部门
            CompanyDept dept=new CompanyDept();
            dept.setCompanyId(company.getCompanyId());
            dept.setParentId(0l);
            dept.setAncestors("0");
            dept.setStatus("0");
            dept.setCreateTime(new Date());
            dept.setDeptName(company.getCompanyName());
            deptMapper.insertCompanyDept(dept);
            //创建岗位
            CompanyPost post=new CompanyPost();
            post.setCompanyId(company.getCompanyId());
            post.setPostCode("manager");
            post.setPostName("管理员");
            post.setPostSort(0);
            post.setStatus("0");
            postMapper.insertCompanyPost(post);
            //创建角色
            CompanyRole role=new CompanyRole();
            role.setCompanyId(company.getCompanyId());
            role.setRoleName("管理员");
            role.setRoleKey("admin");
            role.setRoleSort(0);
            role.setStatus("0");
            roleMapper.insertCompanyRole(role);
            //添加用户
            CompanyUser user=new CompanyUser();
            user.setCompanyId(company.getCompanyId());
            user.setNickName("管理员");
            user.setPassword(company.getPassword());
            user.setUserName(company.getUserName());
            user.setDeptId(dept.getDeptId());
            user.setUserType("00");
            user.setPhonenumber(company.getCompanyMobile());
            user.setSex("0");
            user.setStatus("0");
            user.setIsDel(0);
            userMapper.insertCompanyUser(user);
            //添加用户角色表
            CompanyUserRole userRole=new CompanyUserRole();
            userRole.setRoleId(role.getRoleId());
            userRole.setUserId(user.getUserId());
            userRoleMapper.insertCompanyUserRole(userRole);
            //添加用户岗位表
            CompanyUserPost userPost=new CompanyUserPost();
            userPost.setPostId(post.getPostId());
            userPost.setUserId(user.getUserId());
            userPostMapper.insertCompanyUserPost(userPost);
            company.setUserId(user.getUserId());
            companyMapper.updateCompany(company);

            return R.ok();
        }
        else
        {
            return R.error("操作失败");
        }
    }

    /**
     * 修改企业
     *
     * @param company 企业
     * @return 结果
     */
    @Override
    public int updateCompany(Company company)
    {
        company.setUpdateTime(DateUtils.getNowDate());
        return companyMapper.updateCompany(company);
    }

    /**
     * 批量删除企业
     *
     * @param companyIds 需要删除的企业ID
     * @return 结果
     */
    @Override
    public int deleteCompanyByIds(Long[] companyIds)
    {
        return companyMapper.deleteCompanyByIds(companyIds);
    }

    /**
     * 删除企业信息
     *
     * @param companyId 企业ID
     * @return 结果
     */
    @Override
    public int deleteCompanyById(Long companyId)
    {
        return companyMapper.deleteCompanyById(companyId);
    }

    @Override
    public List<CompanyVO> selectCompanyVOList(CompanyParam param) {
        return companyMapper.selectCompanyVOList(param);
    }



    @Override
    public List<CompanyOptionRespVO> selectCompanyOptions(String companyName) {
        return companyMapper.selectCompanyOptions(companyName);
    }

    @Override
    public List<CompanyVO> selectCompanyAll() {
        return companyMapper.selectCompanyAll();
    }
}
