package com.bohuikeji.delivery.backstage.controller.api.share;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bohuikeji.delivery.backstage.entity.TComplaint;
import com.bohuikeji.delivery.backstage.entity.TLogistics;
import com.bohuikeji.delivery.backstage.service.ITComplaintService;
import com.bohuikeji.delivery.backstage.service.ITLogisticsService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.back.DataPage;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;


/**
 * 投诉记录
 */
@Api(tags = {"投诉记录接口"})
@RestController
@RequestMapping("/api/complaint")
public class ComplaintApi extends ApiController {

    @Autowired
    ITComplaintService complaintService;
    @Autowired
    ITOrderService orderService;
    @Autowired
    ISysConfigService sysConfigService;
    @Autowired
    ITLogisticsService logisticsService;


    /**
     * 派送员查询投诉记录
     * @return
     */
    @ApiOperation("派送员查询投诉记录")
    @GetMapping("/getTcmpliant")
    //配置忽略参数
    @ApiOperationSupport(ignoreParameters = {"records","total","searchCount"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="courierId",value="派送员登陆id",dataType="Integer", paramType = "query")})
    public APIEntity<IPage<TComplaint>> getTcmpliant(DataPage page, Integer courierId){
        return APIEntity.success(complaintService.page(page,new QueryWrapper<TComplaint>().eq("complain_person_id",courierId)));
    }

    /**
     * 对派送员的进行投诉
     * @param customerId
     * @param complaintDesc
     * @param complaintEvidence
     * @param complaintCauses
     * @param logisticsNum
     * @return
     */
    @ApiOperation("对派送员的投诉记录")
    @PostMapping("/record")
    @ApiImplicitParams({
            @ApiImplicitParam(name="customerId",value="客户id",dataType="Integer", paramType = "query",example="1",required = true),
            @ApiImplicitParam(name="complaintDesc",value="投诉描述",dataType="String", paramType = "query",example="原因"),
            @ApiImplicitParam(name="complaintEvidence",value="投诉佐证附件",dataType="String", paramType = "query",example="图片"),
            @ApiImplicitParam(name="complaintCauses",value="投诉原因",dataType="String", paramType = "query",example="投诉原因"),
            @ApiImplicitParam(name="logisticsNum",value="投诉订单物流号",dataType="String", paramType = "query",example="投诉订单物流号",required = true)})
    public APIEntity record( Integer customerId,String complaintDesc,String complaintEvidence,String complaintCauses,String logisticsNum){
        TLogistics logistics = logisticsService.getOne(new QueryWrapper<TLogistics>().eq("logistics_status","已完成").eq("logistics_num",logisticsNum).eq("player_id",customerId));
        //SysConfig sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("config_name",sysConfigService));
        TComplaint tComplaint = new TComplaint();
        if(customerId != null){
            tComplaint.setComplaintSign("t_customer");
            tComplaint.setOrderNum(logistics.getOrderNum());
            tComplaint.setComplaintTime(LocalDateTime.now());
            tComplaint.setComplaintId(customerId);
            tComplaint.setComplaintDesc(complaintDesc);
            tComplaint.setComplaintEvidence(complaintEvidence);
            tComplaint.setComplaintCauses(complaintCauses);
            tComplaint.setLogisticsNum(logisticsNum);
            //tComplaint.setComplaintLevel(sysConfig.getConfigValue());
            tComplaint.setComplaintState("待审核");
            complaintService.save(tComplaint);
        }
        return APIEntity.success(tComplaint);
    }

    /**
     * 客户个人投诉建议
     * @param tComplaint
     * @return
     */
    @ApiOperation("客户个人投诉建议")
    @PostMapping("/addProposal")
    public APIEntity addProposal(TComplaint tComplaint){
        return APIEntity.success(complaintService.save(tComplaint));
    }
    
}
