package com.crm.manager.controller;

import com.crm.login.rely.backend.controller.BaseLoginController;
import com.crm.manager.service.SysQaService;
import com.crm.manager.service.UserRoleService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.system.qa.SysQaDto;
import com.crm.rely.backend.core.dto.user.role.UserAllRoleDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.system.qa.SysQaAddEntity;
import com.crm.rely.backend.core.entity.system.qa.SysQaDeleteEntity;
import com.crm.rely.backend.core.entity.system.qa.SysQaSearchPageEntity;
import com.crm.rely.backend.core.entity.system.qa.SysQaUpdateEntity;
import com.crm.rely.backend.core.entity.user.role.qa.UserRoleQaSaveEntity;
import com.crm.rely.backend.util.GetIpAndMac;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/qa")
public class SysQaController extends BaseLoginController {

    @Autowired
    private SysQaService service;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 添加
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysQaAddEntity entity, HttpServletRequest request,
                             InfoEntity infoEntity) throws Exception {
        String ip = GetIpAndMac.getIp(request);
        Date time = new Date();
        Long userId = infoEntity.getIbInfo().getId();

        for (UserRoleQaSaveEntity userRoleQaSaveEntity : entity.getRoleIds()) {
            userRoleQaSaveEntity.setIp(ip);
            userRoleQaSaveEntity.setTime(time);
            userRoleQaSaveEntity.setUserId(userId);
            userRoleQaSaveEntity.setId(null);
        }
        service.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysQaDeleteEntity entity) throws Exception {

        service.delete(entity);
        return BaseResultDto.success();

    }

    /**
     * 修改
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysQaUpdateEntity entity, HttpServletRequest request,
                                InfoEntity infoEntity) throws Exception {
        String ip = GetIpAndMac.getIp(request);
        Date time = new Date();
        Long userId = infoEntity.getIbInfo().getId();

        for (UserRoleQaSaveEntity userRoleQaSaveEntity : entity.getRoleIds()) {
            userRoleQaSaveEntity.setIp(ip);
            userRoleQaSaveEntity.setTime(time);
            userRoleQaSaveEntity.setUserId(userId);
        }
        service.update(entity);
        return BaseResultDto.success();

    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto list(@RequestBody @Validated SysQaSearchPageEntity entity, InfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
            entity.setRoleId(null);
        } else {
            entity.setRoleId(infoEntity.getIbInfo().getRoleId());
        }
        ResultWithPagerDto<SysQaDto> sysSourceDtoResultWithPagerDto = service.pageSearchList(entity);
        return sysSourceDtoResultWithPagerDto;

    }

    @PostMapping("/search/single")
    public BaseResultDto single(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        SysQaDto dto = service.searchSingle(entity.getId());
        return BaseResultDto.success(dto);

    }

    @PostMapping("/{method}/role/list")
    public BaseResultDto list(@PathVariable("method") String method, InfoEntity infoEntity) throws Exception {
        if (!"add".equals(method) && !"update".equals(method)) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        Integer merchantId = infoEntity.getIbInfo().getMerchantId();
        List<UserAllRoleDto> dtos = userRoleService.getListInfo(merchantId);
        return BaseResultDto.success(dtos);

    }
}
