package com.zhiqiyun.open.mvc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiqiyun.open.annotation.Permission;
import com.zhiqiyun.open.core.enmus.ComplaintState;
import com.zhiqiyun.open.core.models.statistics.ComplaintInfo;
import com.zhiqiyun.open.core.service.ComplaintInfoService;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.mvc.Result;
import com.zhiqiyun.open.mvc.params.statistics.QueryComplaintInfoParams;
import com.zhiqiyun.open.mvc.params.statistics.SaveComplaintInfoParams;
import com.zhiqiyun.open.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/statistics/complaint")
public class ComplaintController {

    @Autowired
    private ComplaintInfoService complaintInfoService;

    @Autowired
    private SequenceService sequenceService;

    @Permission(value = "statistics.complaint", tags = "查询客诉信息")
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryComplaintInfoParams params) {

        QueryWrapper<ComplaintInfo> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(params.getTitle())) {
            wrapper.like("title", params.getTitle());
        }
        if (StringUtils.isNotBlank(params.getCategory())) {
            wrapper.like("category", params.getCategory());
        }
        if (StringUtils.isNotBlank(params.getChannel())) {
            wrapper.like("channel", params.getChannel());
        }
        if (params.getState() != null) {
            wrapper.eq("state", params.getState());
        }
        if (params.getCreatedTime() != null) {
            wrapper.between("created_time", params.getCreatedTime().getStatDate(), params.getCreatedTime().getEndDate());
        }
        if (params.getProcessedTime() != null) {
            wrapper.between("processed_time", params.getProcessedTime().getStatDate(), params.getProcessedTime().getEndDate());
        }

        Page<ComplaintInfo> page = params.getPage();
        page.addOrder(OrderItem.desc("id"));

        Page<ComplaintInfo> resultData = this.complaintInfoService.page(page, wrapper);

        return Result.instance(Result.Code.SUCCESS).setData(resultData);
    }

    @Permission(value = "statistics.complaint", tags = "保存客诉信息")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SaveComplaintInfoParams params) {

        this.complaintInfoService.save(this.buildEntity(this.sequenceService.nextId(), params));

        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "statistics.complaint", tags = "编辑客诉信息")
    @PostMapping("/updateById")
    public Result updateById(Long id, @Valid @RequestBody SaveComplaintInfoParams params) {

        this.complaintInfoService.updateById(this.buildEntity(id, params));

        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    private ComplaintInfo buildEntity(Long id, @Valid SaveComplaintInfoParams params) {

        ComplaintInfo entity = new ComplaintInfo();
        BeanUtils.copyProperties(params, entity);

        entity.setState(ComplaintState.PENDING);
        entity.setCreatedTime(DateUtil.current());
        entity.setId(id);
        return entity;
    }

    @Permission(value = "statistics.complaint", tags = "删除客诉信息")
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> ids) {
        this.complaintInfoService.removeByIds(ids);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "statistics.complaint", tags = "设置客诉为已处理")
    @PostMapping("/processedByIds")
    public Result processedByIds(@RequestBody List<Long> ids) {
        ComplaintInfo entity = new ComplaintInfo();
        entity.setState(ComplaintState.PROCESSED);
        entity.setProcessedTime(DateUtil.current());
        QueryWrapper<ComplaintInfo> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        this.complaintInfoService.update(entity, wrapper);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }
}
