package cn.ey.biz.module.system.api.contract;

import cn.ey.biz.common.pojo.PageParam;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.api.contract.dto.ContractArchivedDTO;
import cn.ey.biz.module.system.api.contract.dto.ContractFieldDTO;
import cn.ey.biz.module.system.api.contract.dto.ContractSaveDTO;
import cn.ey.biz.module.system.api.contract.dto.ContractTypeDTO;
import cn.ey.biz.module.system.controller.admin.contract.v2.vo.ContractArchivedVO;
import cn.ey.biz.module.system.controller.admin.contract.vo.ContractSaveVO;
import cn.ey.biz.module.system.convert.contract.ContractArchivedConvert;
import cn.ey.biz.module.system.convert.contract.ContractConvert;
import cn.ey.biz.module.system.convert.user.UserConvert;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import cn.ey.biz.module.system.enums.contract.ContractTypeEnum;
import cn.ey.biz.module.system.service.contract.ContractArchivedFieldService;
import cn.ey.biz.module.system.service.contract.ContractArchivedService;
import cn.ey.biz.module.system.service.contract.ContractService;
import cn.ey.biz.module.system.service.permission.DataRoleService;
import jodd.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

import static cn.ey.biz.module.system.enums.ApiConstants.VERSION;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class ContractApiImpl implements ContractApi {

    @Autowired
    private ContractService contractService;

    @Autowired
    private ContractArchivedFieldService contractArchivedFieldService;

    @Autowired
    private ContractArchivedService contractArchivedService;

    @Autowired
    private DataRoleService dataRoleService;

    @Override
    public R<List<ContractTypeDTO>> getTypes(String sl) {
        return R.success(contractService.getTypes(sl));
    }

    @Override
    public R<List<ContractFieldDTO>> getContractArchivedFields(ContractFieldDTO info) {
        return R.success(contractArchivedFieldService.getContractArchivedFieldBySl(info));
    }

    @Override
    public R<ContractArchivedDTO> getContractArchivedEp(ContractArchivedDTO info) {
        ContractArchivedVO vo = contractArchivedService.getContractArchived(info.getSl(),
                info.getMr(), info.getGeo(), info.getType());
        if (Objects.isNull(vo)) {
            return R.success(null);
        }
        ContractArchivedDTO dto = ContractArchivedConvert.INSTANCE.vo2Dto(vo);
        List<AdminUserDO> users = dataRoleService.getUsersByDataRoleCode(dto.getDataRole());
        dto.setUsers(UserConvert.INSTANCE.convertList4(users));
        return R.success(dto);
    }

    @Override
    public R<List<String>> getSl(String name) {
        return R.success(contractService.getSl(name));
    }

    @Override
    public R<PageResult<?>> getContractPage(ContractSaveDTO<PageParam> pageCon) {
        ContractSaveVO<PageParam> page = ContractConvert.INSTANCE.convert(pageCon);
        PageResult<?> ret = new PageResult<>();
        if (StringUtil.isNotBlank(page.getType())
                && ContractTypeEnum.CONTRACT_TYPE.getType().equals(page.getType())) {
            ret = contractService.getContractTypePage(page);
        }
        return R.success(ret);
    }

}
