package com.yunzhi.marketing.xlk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.StringUtils;
import com.yunzhi.marketing.xlk.dto.InstitutionDTO;
import com.yunzhi.marketing.xlk.entity.Institution;
import com.yunzhi.marketing.xlk.service.IInstitutionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
    * 组织结构表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2021-06-22
 */
@RestController
@RequestMapping("/api")
@Api(value = "xlk-组织结构相关接口", tags = "xlk-组织结构相关接口")
public class InstitutionController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(InstitutionController.class);

    @Autowired
    public IInstitutionService iInstitutionService;


    /**
     * 分页查询列表
     * @param institutionCode
     * @return
     */
    @ApiOperation(value = "admin-组织机构列表查询", notes = "admin-组织机构列表查询")
    @RequestMapping(value="/admin/institution/list",method= RequestMethod.GET)
    public ResponseBean institutionList(@RequestParam(value ="institutionCode",required = false) String institutionCode,
                                        @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            LambdaQueryWrapper<Institution> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Institution::getOrgId,getOrgId(request));
            if (StringUtils.isEmpty(institutionCode)){
                queryWrapper.eq(Institution::getType,1);
            }else {
                Institution institution = iInstitutionService.getById(institutionCode);
                queryWrapper.eq(Institution::getType,institution.getType() + 1);
                queryWrapper.likeRight(Institution::getInstitutionCode,institution.getInstitutionCode());
            }
            queryWrapper.orderByDesc(Institution::getCreateDate);

            List<Institution> list = iInstitutionService.list(queryWrapper);
            responseBean.addSuccess(list);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("institutionList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param institutionDTO 实体对象
     * @return
     */
    @ApiOperation(value = "admin-组织机构列表查询", notes = "admin-组织机构列表查询")
    @RequestMapping(value="/admin/institution/save",method= RequestMethod.POST)
    public ResponseBean institutionAdd(@RequestBody @Valid InstitutionDTO institutionDTO, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer orgId = getOrgId(request);
            Institution institution = new Institution();
            BeanUtils.copyProperties(institutionDTO,institution);
            institution.setOrgId(orgId);
            institution.setCreateDate(LocalDateTime.now());
            responseBean = iInstitutionService.saveInstitution(institution);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("institutionAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ApiOperation(value = "admin-组织机构列表删除", notes = "admin-组织机构列表删除")
    @ResponseBody
    @RequestMapping(value="/admin/institution/{id}", method= RequestMethod.DELETE)
    public ResponseBean institutionDelete(@PathVariable String id, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iInstitutionService.deleteByinstitutionCode(id);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("institutionDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param institutionDTO 实体对象
     * @return
     */
    @ApiOperation(value = "admin-组织机构列表修改", notes = "admin-组织机构列表修改")
    @RequestMapping(value="/admin/institution/{id}",method= RequestMethod.PUT)
    public ResponseBean institutionUpdate(@PathVariable String id,
                                          @RequestBody InstitutionDTO institutionDTO, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer orgId = getOrgId(request);
            Institution institution = new Institution();
            BeanUtils.copyProperties(institutionDTO,institution);
            institution.setOrgId(orgId);
            institution.setInstitutionCode(id);
             responseBean = iInstitutionService.updateInstitution(institution);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("institutionUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @ApiOperation(value = "admin-组织机构列表查询详细", notes = "组织机构列表查询详细")
    @RequestMapping(value="/admin/institution/{id}",method= RequestMethod.GET)
    public ResponseBean institutionGet(@PathVariable String id, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iInstitutionService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("institutionDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
