package com.lightinit.hsdataplatform.impl;

import com.lightinit.hsdataplatform.entity.ActivityExample;
import com.lightinit.hsdataplatform.entity.Admin;
import com.lightinit.hsdataplatform.entity.AdminExample;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.mapper.AdminMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.AdminViewModel;
import com.lightinit.hsdataplatform.model.admin.UserAddModel;
import com.lightinit.hsdataplatform.model.admin.UserAdminSearchModel;
import com.lightinit.hsdataplatform.model.admin.UserEditModel;
import com.lightinit.hsdataplatform.service.IAdminService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Dictionary;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/12.
 */
@Service
public class AdminServiceImpl implements IAdminService{
    @Autowired
    private AdminMapper adminMapper;

    public Admin SelectOne(String username) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andLockEqualTo(0);
        List<Admin> list=adminMapper.selectByExample(example);
        if(list!=null && list.size()>0)
            return list.get(0);
        return null;
    }

    public Admin QueryOne(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return adminMapper.selectByPrimaryKey(id);
    }

    public List<Admin> QueryList(Dictionary<String, String> condition, String order, int page, int rows) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        //AdminExample.Criteria criteria=example.createCriteria();
        BaseExample.Page _page=new BaseExample.Page();
        _page.setPageNo(page);
        _page.setPageSize(rows);
        example.setPage(_page);
        return adminMapper.selectByExample(example);
    }

    public long QueryCount(Dictionary<String, String> condition, String order) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        //AdminExample.Criteria criteria=example.createCriteria();
        return adminMapper.countByExample(example);
    }

    public boolean AddAdmin(Admin entity) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return adminMapper.insert(entity)>0;
    }

    public boolean UpdateAdmin(Admin entity) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(entity.getId());
        return adminMapper.updateByExample(entity,example)>0;
    }

    public boolean IsExits(String username) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andDeletedAtIsNull();
        criteria.andUsernameEqualTo(username);
        return adminMapper.countByExample(example)>0;
    }

    public boolean Delete(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(id);
        return adminMapper.deleteByExample(example)>0;
    }

    @Override
    public AdminViewModel getAdminViewModel(Long id) {
        return adminMapper.getAdminViewModel(id);
    }

//    ------------------_______--------_______----------------__-----____----------____________---_---_--_-_--______________--_--__

    @Override
    public List<Admin> queryList(UserAdminSearchModel inputModel, BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        if (inputModel != null){
            AdminExample.Criteria criteria = example.createCriteria() ;
            if (StringUtils.isNotEmpty(inputModel.getShowname())){
                criteria.andShownameLike("%" + inputModel.getShowname() + "%");
            }
            if (StringUtils.isNotEmpty(inputModel.getUsername())){
                criteria.andUsernameLike("%"+inputModel.getUsername()+"%");
            }
                criteria.andUsernameNotEqualTo("admin");
                criteria.andDeletedAtIsNull();
        }
        example.setPage(pager);
        return adminMapper.selectByExample(example);
    }

    @Override
    public long queryCount(UserAdminSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        if (inputModel != null){
            AdminExample.Criteria criteria=example.createCriteria();
            if (StringUtils.isNotEmpty(inputModel.getUsername())){
                criteria.andUsernameLike("%" + inputModel.getUsername() + "%");
            }
            if (StringUtils.isNotEmpty(inputModel.getShowname())){
                criteria.andShownameLike("%"+inputModel.getShowname()+"%");
            }
            criteria.andUsernameNotEqualTo("admin");
            criteria.andDeletedAtIsNull();
        }
        return adminMapper.countByExample(example);
    }

    @Override
    public ResultState<Long> addAdmin(UserAddModel userAddModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();

        if (IsExits(userAddModel.getUsername())){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("用户名重复,数据插入失败");
        }else {
            String salt = new SecureRandomNumberGenerator().nextBytes().toHex();
            String password_cipherText = new Md5Hash(userAddModel.getPassword(), userAddModel.getUsername() + salt, 3).toHex();
            Admin admin = new Admin();
            BeanUtils.copyProperties(userAddModel, admin);
            admin.setPassword(password_cipherText);
            admin.setSalt(salt);
            admin.setCreatedAt(new Date());
            int result = adminMapper.insert(admin) ;
            if (!(result>0)){
                resultState.setStateCode(ResultStateCode.INVALID_DATA);
                resultState.setMsg("数据插入失败");
            }
            resultState.setData(admin.getId());
        }
        return resultState;
    }

    @Override
    public ResultState<Long> editAdmin(UserEditModel userEditModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
            Admin admin = new Admin();
            BeanUtils.copyProperties(userEditModel, admin);
            AdminExample adminExample = new AdminExample() ;
            AdminExample.Criteria criteria = adminExample.createCriteria() ;
            criteria.andIdEqualTo(userEditModel.getId()) ;
            int result = adminMapper.updateByExampleSelective(admin,adminExample) ;
            if (!(result>0)){
                resultState.setStateCode(ResultStateCode.INVALID_DATA);
                resultState.setMsg("数据更新失败");
            }
            resultState.setData(userEditModel.getId());
        return resultState ;
    }

    @Override
    public ResultState<Long> deleteAdmin(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();

        Admin admin = new Admin() ;
        admin.setDeletedAt(new Date());
        AdminExample adminExample = new AdminExample() ;
        AdminExample.Criteria criteria = adminExample.createCriteria() ;
        criteria.andIdEqualTo(id) ;
        int result = adminMapper.updateByExampleSelective(admin,adminExample) ;
        if (!(result>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据删除失败");
        }
        resultState.setData(id);
        return resultState ;
    }
}
