package com.ghy.web.controller.worker;

import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.ghy.common.enums.WorkerStatus;
import com.ghy.common.utils.ExceptionUtil;
import com.ghy.web.pojo.vo.WorkerSettledRequest;
import com.ghy.worker.domain.Worker;
import com.ghy.worker.service.WorkerAreaService;
import com.ghy.worker.service.WorkerGoodsCategoryService;
import com.ghy.worker.service.WorkerService;
import com.ghy.worker.service.WorkerSpecialSkillService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author clunt
 * 师傅端Controller
 */
@Controller
@RequestMapping("/worker")
public class WorkerController extends BaseController {

    private String prefix = "worker";

    @Autowired
    private WorkerService workerService;

    @Autowired
    private WorkerAreaService workerAreaService;

    @Autowired
    private WorkerGoodsCategoryService workerGoodsCategoryService;

    @Autowired
    private WorkerSpecialSkillService specialSkillService;

    @RequiresPermissions("worker:worker:view")
    @GetMapping()
    public String worker(){
        return prefix + "/worker";
    }

    @RequiresPermissions("worker:worker:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Worker worker)
    {
        startPage();
        List<Worker> list = workerService.getWorkList(worker);
        return getDataTable(list);
    }

    @PostMapping("/update")
    @ResponseBody
    public AjaxResult updateWorker(@RequestBody Worker worker){
        try {
            workerService.updateWorker(worker);
            return AjaxResult.success("修改成功");
        }catch (Exception e){
            logger.error(ExceptionUtil.getExceptionMessage(e));
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/settled")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public  AjaxResult settled(@RequestBody WorkerSettledRequest request) {
        try {
            // TODO:参数校验
            // 入驻区域信息持久化
            workerAreaService.updateWorkerServArea(request.getWorkerId(), request.getWorkerAreas());
            // 入驻服务品类信息持久化
            workerGoodsCategoryService.updateWorkerGoodsCategory(request.getWorkerId(), request.getGoodsCategories());
            // 入驻特殊技能信息持久化
            specialSkillService.updateWorkerSpecialSkill(request.getWorkerId(), request.getSpecialSkills());
            // 更新师傅状态为审核中
            Worker worker = new Worker();
            worker.setWorkerId(request.getWorkerId());
            worker.setStatus(WorkerStatus.AUDITING.getCode());
            workerService.updateWorker(worker);
            return AjaxResult.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtil.getExceptionMessage(e));
            return AjaxResult.error(e.getMessage());
        }
    }
}
