package com.zksy.web.controller.basicData;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.controller.BaseController;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import com.zksy.system.basicData.domain.CrmComplain;
import com.zksy.system.basicData.domain.CrmFile;
import com.zksy.system.basicData.domain.dto.CrmComplainDTO;
import com.zksy.system.basicData.service.CrmComplainService;
import com.zksy.system.basicData.service.CrmFileService;
import com.zksy.utils.SearchUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 投诉Controller
 *
 * @author sy
 * @date 2024-05-10
 */
@RestController
@RequestMapping("/crmComplain")
@Api(tags = "投诉", description = "投诉desc")
public class CrmComplainController extends BaseController {

    @Autowired
    private CrmComplainService service;

    @Autowired
    private CrmFileService crmFileService;

    @GetMapping("/getById/{borrowId}")
    @ApiOperation(value = "投诉搜索getById")
    public AjaxResult getById(@PathVariable String borrowId) {
        return AjaxResult.success(service.getByComplainId(borrowId));
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "投诉分页")
    public Page<CrmComplain> findByPage(long pageNum, long pageSize, String conditionJson) throws Exception {
        Page<CrmComplain> page = service.page(new Page<>(pageNum, pageSize), SearchUtil.parseWhereSql(conditionJson));
        for(CrmComplain entity : page.getRecords()){
            List<CrmFile> crmFile = crmFileService.selectByFid(entity.getId());
            if(crmFile != null) {
                entity.setFiles(crmFile);
            }
        }
        return page;
    }

    @GetMapping("/getList")
    @ApiOperation(value = "投诉查询所有")
    public AjaxResult getList(String conditionJson) throws Exception {
        List<CrmComplain> list = service.list(SearchUtil.parseWhereSql(conditionJson));
        for(CrmComplain entity : list){
            List<CrmFile> crmFile = crmFileService.selectByFid(entity.getId());
            if(crmFile != null) {
                entity.setFiles(crmFile);
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 新增投诉
     */
    @PostMapping("/save")
    @ApiOperation(value = "投诉新增")
//    @Log(title = "新增投诉", businessType = BusinessType.INSERT)
    public AjaxResult saveCrmComplain(@RequestBody CrmComplainDTO dto) {
        return service.saveCrmComplain(dto);
    }

    /**
     * 修改投诉
     */
    @PostMapping("/updateById")
    @ApiOperation(value = "投诉修改")
    @Log(title = "修改投诉", businessType = BusinessType.UPDATE)
    public AjaxResult updateById(@RequestBody CrmComplain crmComplain) {
        return service.updateCrmComplain(crmComplain);
    }

    @Log(title = "投诉", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除投诉", notes = "删除投诉")
    public AjaxResult delete(@RequestBody List<String> ids)
    {
        return service.removeCrmComplain(ids);
    }

    @PostMapping("/generateVoucher")
    public void generateVoucher(HttpServletResponse response,String name,Long id) throws Exception {
        service.generateVoucher(response,name,id);
    }

}

