package cn.com.countrygarden.bi.main.service.impl;


import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.sys.FunctionMapper;
import cn.com.countrygarden.bi.main.dao.sys.RoleFunctionsMapper;
import cn.com.countrygarden.bi.main.dto.CheckListDTO;
import cn.com.countrygarden.bi.main.dto.TransferDTO;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.sys.Function;
import cn.com.countrygarden.bi.main.model.sys.RoleFunctions;
import cn.com.countrygarden.bi.main.service.FunctionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
@Transactional
public class FunctionServiceImpl implements FunctionService {
    @Autowired
    private FunctionMapper functionMapper;
    @Autowired
    private RoleFunctionsMapper roleFunctionsMapper;

    @Override
    public List<Function> findByUserCode(String userCode) {

        return functionMapper.findByUserCode(userCode);
    }

    @Override
    public Result queryFunctionsByRole(String roleCode) {
        List<CheckListDTO> checkListDTO=functionMapper.selectCheckListByRole(roleCode);
        return Response.response(checkListDTO);
    }

    @Override
    public Result queryTransferListByRole(String roleCode) {
        TransferDTO dto=new TransferDTO();
        dto.setData(functionMapper.selectAll());
        List<String> value=new ArrayList<>();
        if(StringUtils.isNotBlank(roleCode)){
            value = functionMapper.selectExistFunctionsByRole(roleCode);
        }
        dto.setValue(value);
        return Response.response(dto);
    }

    @Override
    public Result editRolesFunctions(String roleCode, String funCodes, String userCode) {
        List<RoleFunctions> roleFunctionsList=new LinkedList<>();
        String[] funcAry = funCodes.split(",");
//      清除历史操作授权
        roleFunctionsMapper.deleteByRoleCodes(roleCode);
//      授权
        for (String funCode : funcAry) {
            RoleFunctions functions=new RoleFunctions();
            functions.setRoleCode(roleCode);
            functions.setFunctionCode(funCode);
            functions.setCreatedBy(userCode);
            functions.setCreatedDate(new Date());
            roleFunctionsList.add(functions);
        }
        int i = roleFunctionsMapper.insertList(roleFunctionsList);
        if(i!=roleFunctionsList.size()){
            throw new ServiceException("授权操作异常！");
        }
        return Response.response();
    }

    @Override
    public Result queryPageList(Function functions, int page, int size) {
        Page<Function> objects = PageHelper.startPage(page, size);
        Condition condition = new Condition(Function.class);
        Example.Criteria criteria = condition.createCriteria();
        condition.setOrderByClause(" created_date DESC");
        if(StringUtils.isNotBlank(functions.getCode())){
            criteria.andLike("code",String.format("%%%s%%",functions.getCode()));
        }
        if(StringUtils.isNotBlank(functions.getName())){
            criteria.andLike("name",String.format("%%%s%%",functions.getName()));
        }
        functionMapper.selectByCondition(condition);
        PageData pageData = new PageData(objects);
        pageData.setData(objects.getResult());
        return Response.response(pageData);
    }

    @Override
    public Result queryById(Integer id) {
        return Response.response(functionMapper.selectDataByPrimaryKey(id));
    }

    @Override
    public Result add(Function function, AccessToken accessToken) {
        Condition condition = new Condition(Function.class);
        condition.createCriteria().andEqualTo("code", function.getCode());
        int count = functionMapper.selectCountByCondition(condition);
        if(count>0){
            throw new ServiceException("系统异常，操作权限编码已存在！");
        }
        function.setEnable(true);
        function.setCreatedBy(accessToken.getUserCode());
        function.setCreatedDate(new Date());
        functionMapper.insert(function);
        return Response.response();
    }

    @Override
    public Result edit(Function function, AccessToken accessToken) {
//        Condition condition = new Condition(Function.class);
//        condition.createCriteria().andEqualTo("code", function.getCode());
//        int count = functionMapper.selectCountByCondition(condition);
//        if(count>0){
//            throw new ServiceException("系统异常，操作权限编码已存在！");
//        }
        function.setUpdatedBy(accessToken.getUserCode());
        function.setUpdatedDate(new Date());
        functionMapper.updateByPrimaryKey(function);
        return Response.response();
    }

    @Override
    public Result del(Integer id) {
//      关联授权数据不做任何处理，在用户重新授权时，该脏数据会自动处理
        functionMapper.deleteByPrimaryKey(id);
        return Response.response();
    }
}
