package com.jkcredit.invoice.service.user.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkcredit.invoice.mapper.customer.CustomerMapper;
import com.jkcredit.invoice.mapper.user.UserMapper;
import com.jkcredit.invoice.model.entity.customer.Customer;
import com.jkcredit.invoice.model.entity.user.Role;
import com.jkcredit.invoice.model.entity.user.User;
import com.jkcredit.invoice.service.customer.CustomerService;
import com.jkcredit.invoice.service.user.RoleService;
import com.jkcredit.invoice.service.user.UserService;
import com.jkcredit.invoice.util.DataSignUtil;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.jkcredit.invoice.common.CommonConst.ONE;

/**
 * @description:
 * @author: sunzhaoning
 * @create: 2019-05-28 17:53
 * @version: V1.0
 **/
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    private RoleService roleService;

    @Override
    public IPage getUserWithRolePage(Page page, User user) {
        return baseMapper.getUsersPage(page, user);
    }

    @Override
    public List<User> getAllUser() {
        return baseMapper.getAllUser();
    }

    @Override
    public Boolean deleteUserById(User user) {
        this.removeById(user.getId());
        return Boolean.TRUE;
    }

    @Override
    public RespR<Boolean> updateUserInfo(User user) {
        if (!ONE.equals(user.getRoleId())) {
            //更新客户企业主体
            Customer customer = customerMapper.selectByCustomerName(user.getUserName());
            customer.setCompany(user.getCompany());
            customerMapper.updateByPrimaryKeySelective(customer);
        }
        return new RespR<>(baseMapper.updateUserInfo(user));
    }

    @Override
    public RespR<Boolean> updateCompanyNumById(User user) {
        return new RespR<>(baseMapper.updateCompanyNumById(user));
    }

    @Override
    public User selectUserById(Integer id) {
        User user = baseMapper.getUserById(id);
        Customer customer = customerMapper.selectByCustomerName(user.getUserName());
        user.setFee(customer.getAccountBalance());
        return user;
    }

    @Override
    public User selectUserByUserName(String userName) {
        return baseMapper.getUserByUserName(userName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveUser(User user) {
        //根据角色ID查询角色信息
        Role role = roleService.getById(user.getRoleId());
        //设置角色名称
        user.setRoleName(role.getRoleName());
        //设置创建时间
        user.setCreateTime(System.currentTimeMillis());
        //如果是自有车或无车需要新增客户信息
        if (!ONE.equals(user.getRoleId())) {
            Customer customer = new Customer();
            customer.setCustomerName(user.getUserName());
            customer.setCompany(user.getCompany());
            customer.setFee(user.getFee());
            customer.setBussinessType(Integer.valueOf(user.getRoleId()));
            customer.setAccstatus(0);
            customer.setAppSecret(DataSignUtil.getAppSecret(user.getUserName()));
            customerService.addCustomers(customer);
        }
        this.save(user);
        return Boolean.TRUE;
    }

    @Override
    public RespR<Boolean> restPassword(User user) {
        return new RespR<>(baseMapper.updatePassword(user));
    }

    @Override
    public RespR<Boolean> updateLock(User user) {
        return new RespR<>(baseMapper.updateLock(user));
    }
}