package org.springblade.estate.feign;

import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.estate.dto.NoticeDTO;
import org.springblade.estate.entity.NoticePushRecord;
import org.springblade.estate.service.INoticePushRecordService;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.estate.entity.Notice;
import org.springblade.estate.vo.NoticeVO;
import org.springblade.estate.wrapper.NoticeWrapper;
import org.springblade.estate.service.INoticeService;
import org.springblade.core.boot.ctrl.BladeController;
import java.util.List;
/**
 *  client
 *
 * @author lianghanqiang
 * @since 2021-04-29
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/notice")

public class NoticeClient implements INoticeClient {

    private final INoticeService noticeService;
    private final INoticePushRecordService noticePushRecordService;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public NoticeVO detail(@SpringQueryMap Notice notice) {
        Notice detail = noticeService.getOne(Condition.getQueryWrapper(notice));
        return NoticeWrapper.build().entityVO(detail);
        }

    /**
     * 查询
     */
    @Override
    @GetMapping("/list")
    public List<NoticeVO> list(@SpringQueryMap Notice notice) {
        return NoticeWrapper.build().listVO(noticeService.list(Condition.getQueryWrapper(notice)));
        }

	@Override
	@GetMapping("/getListByResidentialId")
	public List<Notice> getListByResidentialId(@SpringQueryMap NoticeDTO noticeDTO) {
		return this.noticeService.getListByResidentialId(noticeDTO.getResidentialId());
	}


	/**
     * 新增
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody Notice notice) {
        return noticeService.save(notice);
        }

    /**
     * 修改
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody Notice notice) {
        return noticeService.updateById(notice);
        }

    /**
     * 新增或修改
     */
    @Override
    @PostMapping("/submit")
    public boolean submit(@Valid @RequestBody Notice notice) {
        return noticeService.saveOrUpdate(notice);
        }


    /**
     * 删除
     */
    @Override
    @PostMapping("/remove")
    public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return noticeService.deleteLogic(Func.toLongList(ids));
        }

	@Override
	@PostMapping("/publishNotice")
	public boolean publishNotice(@RequestBody NoticeDTO noticeDTO) {
		boolean b = noticeService.save(noticeDTO);
		if(b){
			Long id = noticeDTO.getId();
			NoticePushRecord noticePushRecord = new NoticePushRecord();
			noticePushRecord.setAgencyId(noticeDTO.getAgencyId());
			noticePushRecord.setResidentialId(noticeDTO.getResidentialId());
			noticePushRecord.setAgencyName(noticeDTO.getAgencyName());
			noticePushRecord.setResidentialName(noticeDTO.getResidentialName());
			noticePushRecord.setNoticeId(id);
			noticePushRecord.setOrgPosition(noticeDTO.getOrgPosition());
			boolean save = noticePushRecordService.save(noticePushRecord);
			return save;
		}
		return false;
	}

	@Override
	@PostMapping("/delete")
	public boolean delete(@RequestParam String ids) {
		List<Long> idList = Func.toLongList(ids);
		for(Long noticeId : idList){
			boolean b = noticePushRecordService.deleteByCondition(null, noticeId, null, null);
			if(b){
				return noticeService.removeById(noticeId);
			}else{
				return false;
			}
		}
		return true;
	}

}
