package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeAppealManage;
import shop.alien.entity.store.dto.LifeAppealManageDto;
import shop.alien.entity.store.vo.LifeAppealManageVo;
import shop.alien.store.service.LifeAppealManageService;
import shop.alien.util.common.FileUpload;

import java.io.IOException;
import java.util.List;

/**
 * 申诉管理
 */
@Api(tags = {"一期-申诉管理"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/appeals")
@RequiredArgsConstructor
public class LifeAppealManageController {

    private final FileUpload fileUpload;

    private final LifeAppealManageService lifeAppealManageService;

    @ApiOperation("获取申诉列表")
    @GetMapping("/getAppeals")
    public R getAppeals(@RequestParam(value = "pageNum", defaultValue = "1") int page,
                        @RequestParam(value = "pageSize", defaultValue = "10") int size,
                        @RequestParam(required = false) String storeName,
                        @RequestParam(required = false) String storeContact,
                        @RequestParam(required = false) String storePhone,
                        @RequestParam(required = false) String appealType) {
        log.info("LifeAppealManageController.getAppeals?page={},size={},storeName={},storeContact={},storePhone={},appealType={}", page, size, storeName, storeContact, storePhone, appealType);
        IPage<LifeAppealManageVo> stores = lifeAppealManageService.getStores(page, size, storeName, storeContact, storePhone, appealType);
        return R.data(stores);
    }

    @GetMapping("/{storeId}")
    public List<LifeAppealManage> getAppealByStoreId(@PathVariable String storeId) {
        log.info("LifeAppealManageController.getAppealByStoreId?storeId={}", storeId);
        return lifeAppealManageService.getAppealByStoreId(storeId);
    }

    @ApiOperation("修改申诉状态")
    @PostMapping("/setStoreState")
    public int setAppealType(@RequestBody LifeAppealManageDto lifeAppealManageDto) {
        log.info("LifeAppealManageController.setAppealType?lifeAppealManageDto={}", lifeAppealManageDto.toString());
        return lifeAppealManageService.setStoreState(lifeAppealManageDto);
    }

    @PostMapping("/setIsRead")
    public int setIsRead(@RequestParam("id") String id) {
        log.info("LifeAppealManageController.setIsRead?id={}", id);
        return lifeAppealManageService.setIsRead(id);
    }

    @ApiOperation("申诉")
    @PostMapping("/addOrUpdateAppeal")
    public R<Boolean> addOrUpdateAppeal(LifeAppealManage store,
                                        @RequestParam(value = "image1", required = false) MultipartFile image1,
                                        @RequestParam(value = "image2", required = false) MultipartFile image2) {
        log.info("LifeAppealManageController.addOrUpdateAppeal?store={}&image1={}&image2={}", store.toString(), image1, image2);
        try {
            String path = "";
            if (image1 != null && !StringUtils.isEmpty(image1.getOriginalFilename())) {
                path = fileUpload.saveImage(image1);
            }
            if (image2 != null && !StringUtils.isEmpty(image2.getOriginalFilename())) {
                String image1Path2 = fileUpload.saveImage(image2);
                if (StringUtils.isEmpty(path)) {
                    path = path + ";" + image1Path2;
                }
            }

            // 添加审核锚点
//            WebAudit webAudit = new WebAudit();
//            webAudit.setId(store.getId());
//            webAudit.setType("3");
//            webAudit.setContent(store.getAppealReason());
//            Integer i = webAuditService.platformAuditRefundTrack(webAudit);
//            if (!(i > 0)) {
//                return R.fail("上传失败");
//            }

            store.setAppealImage(path);
            int cnt = lifeAppealManageService.addOrUpdateStore(store);
            return R.success("上传成功");
        } catch (Exception e) {
            log.error("LifeAppealManageController.addOrUpdateAppeal ERROR Msg={}", e.getMessage());
            return R.fail("上传失败");
        }
    }

    @ApiOperation("商家端-申诉历史")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "appealType", value = "审批状态:1:已同意,2:已驳回,3:待处理", dataType = "Integer", paramType = "query", required = true)})
    @PostMapping("/getAppealHistory")
    public R<IPage<LifeAppealManageVo>> getAppealHistory(Integer pageNum, Integer pageSize, Integer storeId, Integer appealType) {
        return R.data(lifeAppealManageService.getAppealHistory(pageNum, pageSize, storeId, appealType));
    }


    @ApiOperation("申诉导出")
    @GetMapping("/appealsExport")
    public R appealsExport(@RequestParam(required = false) String appealType,
                           @RequestParam(required = false) String storeName,
                           @RequestParam(required = false) String storePhone) throws IOException {
        log.info("LifeAppealManageController.appealsExport");
        String s = lifeAppealManageService.appealsExport(appealType, storeName, storePhone);
        return R.data(s);
    }

    /**
     * 根据申诉主键查询详情（平台申诉管理列表同维度的单条数据）。
     *
     * @param id 评论申诉主键，对应列表中的记录 id
     */
    @ApiOperation("根据id查询申诉详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "申诉id（store_comment_appeal 主键）", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getAppealDetail")
    public R<LifeAppealManageVo> getAppealDetail(@RequestParam("id") Integer id) {
        log.info("LifeAppealManageController.getAppealDetail?id={}", id);
        return lifeAppealManageService.getAppealDetailById(id);
    }
}
