package com.zfire.mall.manager.controller.admin;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.admin.reqDto.AdminCompanyWechatReqBean;
import com.zfire.mall.manager.bean.admin.reqDto.AdminCompanyWechatUpdateReqDto;
import com.zfire.mall.manager.bean.admin.reqDto.AdminUserAddReqBean;
import com.zfire.mall.manager.bean.admin.respDto.AdminCompanyWechatRespPageBean;
import com.zfire.mall.manager.bean.admin.respDto.AdminUserDto;
import com.zfire.mall.manager.commonmapper.AdminMapper;
import com.zfire.mall.manager.enums.RoleTypeEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.admin.AdminRoleLogic;
import com.zfire.mall.manager.logic.admin.AdminUserLogic;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.AdminCompany;
import com.zfire.mall.manager.plus.entity.AdminRole;
import com.zfire.mall.manager.plus.entity.AdminUser;
import com.zfire.mall.manager.plus.service.AdminCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class AdminCompanyLogic {

    @Autowired
    private AdminCompanyService adminCompanyService;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminUserLogic adminUserLogic;
    @Autowired
    AdminRoleLogic adminRoleLogic;
    @Autowired
    CommonLogic commonLogic;


    /**
     * 新增商户
     */
    @Transactional
    public void addCompanyWechat(AdminCompanyWechatReqBean adminCompanyWechat, HttpServletRequest request){

        adminCompanyService.save(adminCompanyWechat);

        AdminUser adminUser = adminCompanyWechat.getAdminUser();
        adminUser.setIsMaster(true);
        adminUser.setType(RoleTypeEnum.COMPANY.getCode());
        adminUser.setNickName(adminCompanyWechat.getAdminCompanyName());
        adminUser.setAdminCompanyId(adminCompanyWechat.getAdminCompanyId());
        adminUser.setAdminCompanyName(adminCompanyWechat.getAdminCompanyName());

        AdminUserAddReqBean adminUserAddReqBean = new AdminUserAddReqBean();
        BeanUtils.copyProperties(adminUser,adminUserAddReqBean);
        //添加企业角色
        AdminRole companyRole = adminRoleLogic.getCompanyRole();
        adminUserAddReqBean.setRoleId(companyRole.getAdminRoleId());
        adminUserAddReqBean.setRoleName(companyRole.getName());
        //添加企业用户
        adminUserLogic.add(adminUserAddReqBean, request);
    }


    /**
     * 编辑商户
     */
    public void updateCompanyWechat(AdminCompanyWechatUpdateReqDto updateDto){
        if(StringUtils.isEmpty(updateDto.getCompanyWechatId())){
            throw new RemoteServiceException("商户配置id不能为null");
        }
    }
}
