package org.springblade.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.validator.Validator;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.app.client.ZmtClientService;
import org.springblade.modules.app.common.dto.UserDTO;
import org.springblade.modules.app.common.enums.UserStateEnum;
import org.springblade.modules.app.common.query.AdminUserBaseQuery;
import org.springblade.modules.app.common.query.AdminUserQuery;
import org.springblade.modules.app.common.query.SlaughterHouseQuery;
import org.springblade.modules.app.common.query.UserQuery;
import org.springblade.modules.app.common.vo.*;
import org.springblade.modules.app.constant.Constants;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.UserDO;
import org.springblade.modules.app.entity.VerifyStationDO;
import org.springblade.modules.app.manager.UserManager;
import org.springblade.modules.app.mapper.AppUserMapper;
import org.springblade.modules.app.service.IAppUserService;
import org.springblade.modules.app.utils.ErrorMsg;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author fuxinsheng
 * @since 2021-11-05
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, UserDO> implements IAppUserService {
    @Autowired
    private AppUserMapper userMapper;

    @Autowired
    private UserManager userManager;

    @Autowired
    private ZmtClientService zmtClientService;

    @Override
    public Long addUser(UserDTO userDTO) {
        UserDO userDO = userDTO.copyTo(UserDO.class);
        Validator.isTrue(save(userDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return userDO.getId();
    }

    @Override
    public Page<SlaughterHouseVO> getSlaughterHousePage(SlaughterHouseQuery query) {
        Page<UserDO> page = query.covertToPage();
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.eq(DbField.SLAUGHTER_HOUSE_PARENT_ID, query.getUserId());
        wrapper.orderBy(true, false, DbField.CREATED_TIME);
        page = page(page, wrapper);
        return ModelConvertUtil.copyPageTo(page, SlaughterHouseVO.class);
    }

    @Override
    public UserDTO getUserById(Long id) {
        Validator.notNull(id, ErrorCode.INPUT_PARAMETERS_NULL);
        UserDO userDO = getById(id);
        return userDO == null ? null : userDO.copyTo(UserDTO.class);
    }

    @Override
    public Boolean updateUser(UserDTO userDTO) {
        Validator.notNull(userDTO, ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(userDTO.getId(), ErrorCode.INPUT_PARAMETERS_NULL);
        UserDO userDO = getById(userDTO.getId());
        Validator.notNull(userDO, ErrorMsg.NEW("用户不存在"));
        Validator.isTrue(updateById(userDTO.copyTo(UserDO.class)), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return true;
    }

    @Override
    public UserDTO getOneUser(UserQuery query) {
        Validator.notNull(query, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(query.getName()), DbField.USER_NAME, query.getName());
        wrapper.eq(StringUtils.isNotBlank(query.getPhone()), DbField.PHONE, query.getPhone());
        wrapper.eq(query.getType() != null, DbField.TYPE, query.getType());
        wrapper.last("limit 1");
        UserDO userDO = getOne(wrapper);
        return userDO == null ? null : userDO.copyTo(UserDTO.class);
    }

    @Override
    public List<UserDTO> listUser(UserQuery query) {
        Validator.notNull(query, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(query.getName()), DbField.USER_NAME, query.getName());
        wrapper.eq(StringUtils.isNotBlank(query.getPhone()), DbField.PHONE, query.getPhone());
        wrapper.in(CollectionUtils.isNotEmpty(query.getUserIdList()), DbField.ID, query.getUserIdList());
        wrapper.eq(query.getType() != null, DbField.TYPE, query.getType());
        wrapper.eq(query.getSlaughterHouseParentId() != null, DbField.SLAUGHTER_HOUSE_PARENT_ID, query.getSlaughterHouseParentId());
        List<UserDO> userDOList = list(wrapper);
        return CollectionUtils.isNotEmpty(userDOList) ? ModelConvertUtil.copyListTo(userDOList, UserDTO.class) : null;
    }

    @Override
    public Boolean deleteById(Long id) {
        Validator.notNull(id, ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.isTrue(removeById(id), ErrorCode.DELETE_DATA_ERROR);
        this.update(Wrappers.<UserDO>lambdaUpdate().eq(UserDO::getId,id).set(UserDO::getIsDeleted,1).set(UserDO::getState,6));
        return true;
    }

    @Override
    public Page<UserDTO> pageUser(AdminUserQuery query) {
        Page<UserDO> page = query.covertToPage();
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(query.getPhone()), DbField.PHONE, query.getPhone());
        wrapper.like(StringUtils.isNotBlank(query.getUserName()), DbField.USER_NAME, query.getUserName());
        wrapper.eq(query.getType() != null, DbField.TYPE, query.getType());
        wrapper.eq(query.getState() != null, DbField.STATE, query.getState());
        if (query.getState() == null) {
            wrapper.ne(DbField.STATE, UserStateEnum.UNCHECK.getCode());
        }
        wrapper.le(query.getEndTime() != null, DbField.CREATED_TIME, query.getEndTime());
        wrapper.ge(query.getStartTime() != null, DbField.CREATED_TIME, query.getStartTime());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        page = page(page, wrapper);
        return ModelConvertUtil.copyPageTo(page, UserDTO.class);
    }

    @Override
    public Page<UserDTO> pageRegisterUser(AdminUserQuery userQuery) {
        Page<UserDTO> page = userQuery.covertToPage();
        AdminUserBaseQuery adminUserBaseQuery = ModelConvertUtil.copyTo(userQuery, AdminUserBaseQuery.class);
        return baseMapper.pageRegisterUser(page, adminUserBaseQuery);
    }

    @Override
    public List<UserDTO> listByUserName(String userName) {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(userName), DbField.USER_NAME, userName);
        List<UserDO> userDOList = list(wrapper);
        return CollectionUtils.isNotEmpty(userDOList) ? ModelConvertUtil.copyListTo(userDOList, UserDTO.class) : null;
    }

    @Override
    public UserDTO getUserByIdAll(Long id) {
        Validator.notNull(id, ErrorCode.INPUT_PARAMETERS_NULL);
        UserDO userDO = userMapper.getUserByIdAll(id);
        return userDO == null ? null : userDO.copyTo(UserDTO.class);
    }

    @Override
    public void updateSupplier(){
        UserQuery userQuery = new UserQuery();
        userQuery.setType(1);
        List<UserDTO> userDTOS = listUser(userQuery);
        for (UserDTO userDTO : userDTOS) {
            if (!StringUtil.isEmpty(userDTO.getZmtId()) && userDTO.getZmtId() != 0) {
                OutApiSyncSupplierParamVO supplierParamVO = new OutApiSyncSupplierParamVO();
                supplierParamVO.setOrgId(userDTO.getZmtId());
                List<OutApiSyncSupplierResultVO> eastSupplier = zmtClientService.getEastSupplier(supplierParamVO);
                if (CollectionUtils.isNotEmpty(eastSupplier)) {
                    for (OutApiSyncSupplierResultVO resultVO : eastSupplier) {
                        userManager.saveSupplier(resultVO);
                    }
                }
            }
        }

    }
}
